Java spring boot secutiry-授权用户后调用的方法
在成功验证用户后,我需要在上下文中设置一些内容(更确切地说是数据库上下文持有者)。我已经正确配置了spring安全性。凭据通过头传递(但在这里这并不重要)。Sequent requestsa由会话ID扩展 一切都很好,我只需要实现一些功能,在按照授权接受请求后立即自动调用Java spring boot secutiry-授权用户后调用的方法,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,在成功验证用户后,我需要在上下文中设置一些内容(更确切地说是数据库上下文持有者)。我已经正确配置了spring安全性。凭据通过头传递(但在这里这并不重要)。Sequent requestsa由会话ID扩展 一切都很好,我只需要实现一些功能,在按照授权接受请求后立即自动调用 如何执行?当身份验证成功完成时,Spring Security将触发一个事件。您只需要注册一个事件侦听器来侦听该事件,并提供一个回调,该回调将在触发该事件后执行: @Component class SuccessfulAuth
如何执行?当身份验证成功完成时,Spring Security将触发一个事件。您只需要注册一个事件侦听器来侦听该事件,并提供一个回调,该回调将在触发该事件后执行:
@Component
class SuccessfulAuthenticationListener implements ApplicationListener<AuthenticationSuccessEvent> {
@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
// Put the function which is automatically invoked immediately
// after acceptance of the request as authorized.
}
}
谢谢,像是
onSuccessfullLogin
之类的东西怎么样?好的,但对我来说可以吗?我的意思是,如果它将在每次接受授权请求后调用。是的,因为此筛选器将在安全筛选器链之后注册,isAuthenticated
检查将无效,如果您的所有端点都受到spring security的保护。否则,您将需要此检查以确保仅为受保护的endpointsok调用此筛选器,在我的情况下,所有端点都是安全的,因此无需对进行身份验证。谢谢
@Component
class AuthorizedRequestFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.isAuthenticated()) {
// do whatever is necessary
}
filterChain.doFilter(request, response);
}
}