2011-12-18 1 views
0

사용자 정의 AuthenticationSuccessHandler에서 사용자의 LastLogin 시간을 설정하려고하는데 사용자 이름 검색 방법을 알지 못합니다. (모든 권한 기능 UserDetails로 작업하지 않기 때문에 null을 반환하는 것 같습니다.) 사용자 데이터는 MYSQL 테이블에 저장되며 Hibernate를 사용하여 사용자를 검색/생성/업데이트합니다. 내 응용 프로그램 내에서 스프링 사용자 클래스와 아무 관계가없는 자체 작성된 User 클래스를 사용하고 있습니다. 나는 사용자 정의 된 UserDetails/UserDetailsService도없는 나는 내가 AuthenticationSuccessHandler은 다음과 같습니다 (추가와 같이 값을 추가로)스프링 보안 - UserDetails/UserDetailsService없이 AuthenticationSuccessHandler의 사용자 이름 얻기

를 DB 테이블 레이아웃을 변경할 수 없기 때문에,이를 방지하기 위해 싶습니다

public class PostSuccessfulAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler { 
@Autowired 
private UserService userService; 
@Override 
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
     Authentication authentication) throws ServletException, IOException 
     { 
      userService.trackUserLogin(authentication.getName()); //Doesn't work, getName seems to return null 
      super.onAuthenticationSuccess(request, response, authentication); 
     } 
} 

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
xmlns:p="http://www.springframework.org/schema/p" 
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.xsd"> 

<global-method-security pre-post-annotations="enabled"/> 
<!-- authentication-success-handler-ref='authSuccHandler' --> 
<http use-expressions="true" disable-url-rewriting="true"> 
    <intercept-url pattern="..." access="hasRole('ROLE_USER')" /> 
    <form-login login-page="/index.htm" 
       authentication-failure-handler-ref='authFailureHandler' 
       authentication-success-handler-ref='authSuccHandler' 
       default-target-url='...' 
       always-use-default-target='true'/> 
    <logout logout-success-url="..."/> 
    <session-management invalid-session-url="/index.htm"> 
     <concurrency-control max-sessions="2" error-if-maximum-exceeded="true" /> 
    </session-management> 
</http> 

<authentication-manager> 
    <authentication-provider> 
     <jdbc-user-service data-source-ref="mysqldataSource" 
        authorities-by-username-query="select username, authority from benutzer where username = ?" 
        users-by-username-query="select username, password, enabled from benutzer where username = ?"/> 
    </authentication-provider> 
</authentication-manager> 

</beans:beans> 

이 가지고 있다고 생각하는 날 리드 자체가 (난 그냥 내 AuthenticationSuccessHandler에서 trackUserLogin 라인을 주석 경우에도) 잘 작동 로그인이 얻을 수있는 방법으로 : 내 ApplicationContext를-security.xml은 다음과 같다 그거야. sername. 누구든지 도와 줄 수 있습니까?

+0

설정에 대한 설명이 구성과 일치하지 않는 것 같습니다. 예를 들어, 당신은 Hibernate를 사용하여 사용자를 검색하지만, 설정은 인증을 위해'jdbc-user-service'를 사용하고 ** this는 UserDetailsService이므로 UserDetails로 작업하고 있다고 가정합니다. "모든 권위 함수가 null을 반환하는 것"이라는 말의 의미가 명확하지 않습니다. 'getName()'메서드가 "null을 반환하는 것처럼 보입니다"라고 말하면 실제로는 할 수 없습니다. 아마 코드를 디버그하고 무슨 일이 일어나는지 확인해야합니다. 대신에'ApplicationListener'를 사용하는 것을 볼 수도 있습니다. –

답변

4

시도 authentication.getPrincipal()

관련 문제