我是 Spring 框架和 Spring 安全的新手。我正在使用谷歌应用引擎开发一个应用程序。我正在尝试对用户进行身份验证,但无法实现。我的问题是
- 我需要限制所有用户在浏览器中键入 URL 和查看页面。如果他们想要访问此类页面,我需要将他们导航到警告页面。
- 只有通过应用程序的身份验证,用户才能访问该应用程序。如果未通过身份验证,则应导航登录页面。
- 我需要编写一个自定义登录验证页面,如果给定的凭据是完美的,我应该在页面中验证它们,然后我们可以将它们导航到主页。
- 在自定义登录身份验证页面中,我们应该编写数据库逻辑以从数据库中获取凭据并进行身份验证。如果用户没有在应用程序中注册,那么我们应该使用默认消息将他们导航到注册页面。
请任何人给我一个符合此要求的默认应用程序。
感谢和问候, 查看
最佳答案
我使用的是spring 3.x来完成配置
在 web.xml 中,添加这些行
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
来到security xml页面添加这些。我正在使用 DAOAuthenticationProvider。我正在使用 BCryptPasswordEncoder 进行密码编码。
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http use-expressions="true">
<form-login login-page="/login" always-use-default-target="true" default-target-url="/sessionInit" authentication-failure-url="/login"/>
<logout logout-url="/logout" logout-success-url="/logout"/>
</http>
<beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService" ></beans:property>
</beans:bean>
<beans:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider"/>
</beans:list>
</beans:property>
</beans:bean>
<authentication-manager>
<authentication-provider ref="authProvider"></authentication-provider>
</authentication-manager>
<beans:bean id="authProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService" />
<beans:property name="passwordEncoder" ref="encoder" />
</beans:bean>
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
</beans:beans>
在所有这些配置之前,您需要在 web.xml 中添加这个 security.xml 文件。
关于java - 使用 spring security java 限制用户在查看页面之前进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22804058/