2012-06-11 55 views
0

我正在實現啓用SpringSecurity的Web應用程序。 我現在正在使用PostgreSQL數據庫來存儲用戶及其憑證。 該應用程序的一項要求是每次用戶登錄時更新用戶表(特別是last_login列)。 我試圖實現一個LoginController來擴展UsernamePasswordAuthenticationFilter,所以每次調用successfulAuthentication方法時,都會更新last_login列。用於插入數據庫的AuthenticationFilter

這裏是我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:beans="http://www.springframework.org/schema/beans" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
     xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

<beans:bean class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" id="passwordEncoder"/> 

<beans:bean class="admin.beans.UserBean" id="userBean"/> 

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <beans:property name="basename" value="messages"/> 
</beans:bean> 

<http use-expressions="true" auto-config="true"> 
    <intercept-url pattern="/login.jsp" access="permitAll"/> 
    <intercept-url pattern="/userAdmin.jsp" access="hasRole('ROLE_ADMIN')"/> 
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/> 
    <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/> 
    <remember-me/> 
    <logout invalidate-session="true" logout-success-url="/" logout-url="/logout"/> 
</http> 

<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <beans:property name="driverClassName" value="org.postgresql.Driver"/> 
    <beans:property name="url" value="jdbc:postgresql://xexen:5432/db"/> 
    <beans:property name="username" value="usr"/> 
    <beans:property name="password" value="password"/> 
</beans:bean> 

<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="jdbcUserService"> 
     <password-encoder ref="passwordEncoder"> 
      <salt-source ref="saltSource"/> 
     </password-encoder> 
    </authentication-provider> 
</authentication-manager> 

<beans:bean id="jdbcUserService" class="security.CustomJdbcDaoImpl"> 
    <beans:property name="dataSource" ref="dataSource"/> 
    <beans:property name="enableGroups" value="false"/> 
    <beans:property name="enableAuthorities" value="true"/> 
    <beans:property name="usersByUsernameQuery"> 
     <beans:value>select username,password,enabled, salt from users where username = ?</beans:value> 
    </beans:property> 
</beans:bean> 

<beans:bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy"> 
    <filter-chain-map path-type="ant"> 
     <filter-chain pattern="/**" filters=" authenticationFilter"/> 
    </filter-chain-map> 
</beans:bean> 


<beans:bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource"> 
    <beans:property name="userPropertyToUse" value="salt"/> 
</beans:bean> 

<beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener"/> 

<beans:bean id="authenticationFilter" class="security.LoginController"> 
    <beans:property name="authenticationManager" ref="authenticationManager"/> 
    <beans:property name="filterProcessesUrl" value="/j_spring_security_check"/> 
</beans:bean> 

<context:annotation-config/> 
<context:component-scan base-package="admin"/> 

的LoginController中類:

package security; 
//imports 

@Controller 
public class LoginController extends UsernamePasswordAuthenticationFilter { 

@Autowired 
AuthenticationManager authenticationManager; 

public LoginController() { 
    super(); 
    System.out.println("LoginController.LoginController"); 
} 

@Override 
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException { 
    super.successfulAuthentication(request, response, chain, authResult); 
    System.out.println("Update DB"); 
} 

}

的問題是,該過濾器的LoginController從不調用,從而永遠插入數據庫。 我很確定我是在犯一個配置錯誤,但我都搞砸了,並與配置混淆:在哪裏設置過濾器/提供商等

任何人都可以找到我做錯了什麼? 非常感謝您提前。

回答

1

我想提出一種不同的方法,成功登錄後使用處理程序。
在安全的xml:

<http use-expressions="true" auto-config="true"> 
... 
    <form-login authentication-success-handler-ref="redirectAfterLogin" login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/> 
... 
</http> 

和類會是這樣的:

public class RedirectAfterLogin extends SavedRequestAwareAuthenticationSuccessHandler { 
    public void onAuthenticationSuccess(HttpServletRequest request, 
     HttpServletResponse response, Authentication authentication) 
     throws ServletException, IOException { 

     // do what you need here 
    } 
}