8

我正在使用 Spring 3.2.0 和相同版本的 Spring 安全性。成功登录后,用户将被重定向到受保护的页面之一,如下所示。

public final class LoginSuccessHandler implements AuthenticationSuccessHandler
{
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException
    {
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN"))
        {
            response.sendRedirect("admin_side/Home.htm");
            return;
        }
    }
}

我正在使用休眠。如何在成功登录时更新数据库中的登录日期时间(上次登录)?我在登录页面上有一个提交按钮,其POST请求似乎没有映射到其相应登录控制器中的方法。登录表单的操作实际上映射到 Servlet - j_spring_security_check


整个spring-security.xml文件如下,如果需要的话。

<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http pattern="/Login.htm*" security="none"></http>    

    <http auto-config='true'>
    <!--<remember-me key="myAppKey"/>-->
        <session-management session-fixation-protection="newSession">
            <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
        </session-management>

        <intercept-url pattern="/admin_side/**" access="ROLE_ADMIN" requires-channel="any"/>
        <form-login login-page="/" default-target-url="/admin_side/Home.htm" authentication-failure-url="/LoginFailed.htm" authentication-success-handler-ref="loginSuccessHandler"/>
        <logout logout-success-url="/Login.htm" invalidate-session="true" delete-cookies="JSESSIONID"/>
    </http>

    <authentication-manager>
       <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
               users-by-username-query="select email_id, password, enabled from user_table where lower(email_id)=lower(?)"
               authorities-by-username-query="select ut.email_id, ur.authority from user_table ut, user_roles ur where ut.user_id=ur.user_id and lower(ut.email_id)=lower(?)"/>
       </authentication-provider>
    </authentication-manager>

    <beans:bean id="loginSuccessHandler" class="loginsuccesshandler.LoginSuccessHandler"/>

    <global-method-security>
        <protect-pointcut expression="execution(* dao.*.*(..))" access="ROLE_ADMIN"/>
    </global-method-security>

    <!--<global-method-security secured-annotations="enabled" />-->
</beans:beans>
4

3 回答 3

23

另一种方法是为AuthenticationSuccessEvent.

    @Service
    public class UserService implements
                             ApplicationListener<AuthenticationSuccessEvent> {

        @Override
        public void onApplicationEvent(AuthenticationSuccessEvent event) {
           String userName = ((UserDetails) event.getAuthentication().
                                                  getPrincipal()).getUsername();
           User user = this.userDao.findByLogin(userName);
           user.setLastLoginDate(new Date());
        }
   }
于 2013-03-19T07:10:06.843 回答
6

为什么不直接在身份验证成功处理程序中执行此操作?

public final class LoginSuccessHandler implements AuthenticationSuccessHandler
{
    @Autowired
    private UserService userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException
    {
        String userName = authentication.getPrincipal().getName();
        this.userService.updateLastLoginDateForUserByName(userName);

        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN"))
        {
            response.sendRedirect("admin_side/Home.htm");
            return;
        }
    }
}
于 2013-03-19T09:51:42.603 回答
2

您还可以子类化 springAuthenticationProvider接口并将其注入到<authentication-manager />元素中。

课程将类似于

public class AuthenticationProvider extends DaoAuthenticationProvider {

    // inject whatever tou want

    @Override
    public Authentication authenticate(Authentication authentication)
                throws AuthenticationException {
            return super.authenticate(authentication);

            // do what ever you want here
    }

}

(假设您正在使用DaoAuthenticationProvider

然后你只需要注册bean

<bean class="x.y.z.AuthenticationProvider" id="myAuthProvider" scope="singleton" />
<authentication-manager>
   <authentication-provider ref="myAuthProvider">
        <jdbc-user-service data-source-ref="dataSource"
           users-by-username-query="select email_id, password, enabled from user_table where lower(email_id)=lower(?)"
           authorities-by-username-query="select ut.email_id, ur.authority from user_table ut, user_roles ur where ut.user_id=ur.user_id and lower(ut.email_id)=lower(?)"/>
   </authentication-provider>
</authentication-manager>

(不要相信代码的正确性,我是即时写的。它只是为了展示我的想法。)

斯特凡诺

于 2013-03-19T15:44:28.277 回答