问题描述
我自定义了一个登录页面,登录的action也是自己定义的,主要是去数据库验证用户名和密码是否正确。然后把当前用户存在session中。可发现Security 的http配置完后,拥有权限的用户并不能正确进入页面,都被拦截然后重定向到登录页面。因为初次使用security,有没高手给点意见,哪儿出了问题?截取配置片段:<http><form-login login-page="/demo/user/login" login-processing-url="/demo/user/doLogin" default-target-url="/demo/user/welcome.jsp" authentication-failure-url="/traffic/user/login"/> <intercept-url pattern="/login.jsp" filters="none"/> <intercept-url pattern="/index.jsp" filters="none"/> <intercept-url pattern="/demo/user/login" filters="none"/> <intercept-url pattern="/demo/user/doLogin" filters="none"/> <intercept-url pattern="/demo/user/welcome" filters="none"/> <intercept-url pattern="/demo/user/save" access="ROLE_1" /> <intercept-url pattern="/demo/user/new" access="ROLE_1" /> 问题补充:cwx714 写道
解决方案
登陆页面的form action也可以自定义的,http配置如下:<custom-filter ref="formLoginAuthenticationFilter" position="FORM_LOGIN_FILTER"/>bean配置: <bean id="formLoginAuthenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> <property name="filterProcessesUrl" value="${loginProcessesUrl}"></property></bean>
解决方案二:
新手 标记下!
解决方案三:
引用谢谢cwx714的建议,我目前把登录的action换成"j_spring_security_check"可以登录,可以正常验证。可是我的原来Action中保存Session的那些语句写在哪儿呢?我想不至于用了Security框架,就不用Session了吧。保存Session的那些语句其实可以不用写的,HttpSessionContextIntegrationFilter在执行其他过滤器之前,率先判断用户的session中是否已经存在一个SecurityContext了。如果存在,就把 SecurityContext拿出来,放到SecurityContextHolder中,供Spring Security的其他部分使用。如果不存在,就创建一个SecurityContext出来,还是放到SecurityContextHolder中,供Spring Security的其他部分使用。在所有过滤器执行完毕后,清空SecurityContextHolder,因为SecurityContextHolder是基于ThreadLocal的,如果在操作完成后清空ThreadLocal,会受到服务器的线程池机制的影响。如果你需要在登录后要在session中保存一些用户的其他信息,可以使用AuthenticationProcessingFilter:public class MyAuthenticationProcessingFilter extendsAuthenticationProcessingFilter {@Overridepublic Authentication attemptAuthentication(HttpServletRequest request)throws AuthenticationException { //写你的业务处理 ......return super.attemptAuthentication(request);}然后配置:<bean id="authenticationProcessingFilter" class="com.zbxsoft.ibs.security.service.CaAuthenticationProcessingFilter"><s:custom-filter before="AUTHENTICATION_PROCESSING_FILTER"/> <property name="authenticationFailureUrl" value="/login.action?error=9" /><property name="defaultTargetUrl" value="/index.action" /><property name="authenticationManager" ref="authenticationManager" /></bean><s:authentication-manager alias="authenticationManager" /><bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint"><property name="loginFormUrl" value="/login.action" /><property name="forceHttps" value="false" /></bean>
解决方案四:
在开头<sec:http auto-config="true" use-expressions="true">添加开启表达式.然后加一句<s:intercept-url pattern="/" access="isAuthenticated()" />试试表示拥有权限的人能访问 "/" 下所有的资源
解决方案五:
登录页面的form里是要写j_spring_security_check的,先试试不用数据库登录,成功后把authentication-manager的配置更换成用数据库登录再试:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:s="http://www.springframework.org/schema/security"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.0.3.xsd"default-lazy-init="true"><description>SpringSecurity安全配置</description><s:http auto-config="true"> <s:intercept-url pattern="/login.jsp" filters="none"/> <s:intercept-url pattern="/index.jsp" filters="none"/> <s:intercept-url pattern="/demo/user/login" filters="none"/> <s:intercept-url pattern="/demo/user/doLogin" filters="none"/> <s:intercept-url pattern="/demo/user/welcome" filters="none"/> <s:intercept-url pattern="/demo/user/save" access="ROLE_1" /> <s:intercept-url pattern="/demo/user/new" access="ROLE_1" /> <s:form-login login-page="/login.action" default-target-url="/"authentication-failure-url="/login.action?error=1" /><s:logout logout-success-url="/login.action" /><!--s:remember-me key="e37f4b31-0c45-11dd-bd0b-0800200c9a66" /><s:concurrent-session-control max-sessions="1"exception-if-maximum-exceeded="false" expired-url="/login.action?error=3" /--></s:http><s:authentication-manager alias="authenticationManager"><s:authentication-provider><s:user-service><s:user name="admin" password="admin" authorities="ROLE_1, ROLE_2" /><s:user name="user" password="user" authorities="ROLE_1" /></s:user-service></s:authentication-provider></s:authentication-manager><!--s:authentication-manager alias="authenticationManager"><s:authentication-provider user-service-ref="userDetailsService"><s:password-encoder hash="plaintext" /></s:authentication-provider></s:authentication-manager><bean id="userDetailsService" class="com.zbxsoft.bid.service.UserDetailsServiceImpl" /--> </beans>