首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧安全自动登录拦截

弹簧安全自动登录拦截
EN

Stack Overflow用户
提问于 2013-01-16 08:25:19
回答 2查看 2.5K关注 0票数 3

我已经开发了一个带有Security的注册和登录模块。我现在关心的是如何拦截自动存储的登录以将信息保存在数据库中。我的意思是,当用户标记“记住我”时,如果进入我的应用程序,自动进入登录主页,但我想在数据库中注册访问权限。

现在,当用户显式地浏览登录页面时,很容易做到这一点,但在上述情况下则不然。

致以敬意,

更新:我放了一些额外的信息:

  • security.xml
  • userServiceImpl @Service @Transactional公共类UserServiceImpl实现UserDetailsService { @Resource私有UserDao userDao;public UserDetails loadUserByUsername(字符串用户名)抛出UsernameNotFoundException { List AUTHORITIES = new ArrayList();String密码=userDao.getUserPassword(用户名);if (密码!=null){userDao.registerAccess(用户名);AUTHORITIES.add(新SimpleGrantedAuthority("ROLE_REGISTERED"));返回新用户(用户名、密码、权限);}{引发新的UsernameNotFoundException(“未找到用户:”+用户名);}} }
EN

回答 2

Stack Overflow用户

发布于 2013-01-16 09:03:02

你可以做这样的事

代码语言:javascript
复制
@Component
public class AppListener implements ApplicationListener {


    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof InteractiveAuthenticationSuccessEvent) {
            handleLoginEvent();
        } else if (event instanceof HttpSessionDestroyedEvent)
            handleLogoutEvent((HttpSessionDestroyedEvent) event);

    }

    private void handleLoginEvent() {
      // handle login event
    }

    private synchronized void handleLogoutEvent(HttpSessionDestroyedEvent event) {
        // handle logout event
    }

}

致以敬意,

EDIT

将此添加到web.xml中

代码语言:javascript
复制
  <listener>
         <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
  </listener>
票数 1
EN

Stack Overflow用户

发布于 2013-01-16 09:05:42

您在这里有多个选项:

  • 设置您的org.springframework.security.web.authentication.AuthenticationSuccessHandler
  • 订阅org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent (见@Ionut答案)

AuthenticationSuccessHandler将同样适用于两种情况下的(正常登录并记住我):

代码语言:javascript
复制
public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
    Authentication authentication) throws ServletException, IOException {
        // log authentication success here for both cases
        super.onAuthenticationSuccess(request, response, authentication);
    }

}

在你的security.xml

代码语言:javascript
复制
<bean id="customAuthenticationSuccessHandler" class="com.domain.security.CustomAuthenticationSuccessHandler"/>


<security:http ... >
    ...
    <security:form-login login-page='/login.html' authentication-success-handler-ref="customAuthenticationSuccessHandler" />
    <security:remember-me authentication-success-handler-ref="customAuthenticationSuccessHandler" />

</security:http>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14354034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档