Java 某些页面上的Spring安全性重新身份验证

Java 某些页面上的Spring安全性重新身份验证,java,spring,spring-security,Java,Spring,Spring Security,当用户在特定页面上执行一些非常敏感的操作时,有没有办法强制使用Spring security重新验证?使用 SecurityContextHolder.clearContext(); 或 及 如果您使用的是基于会话的身份验证。为此,您可以使用类似的方法: @Override protected void configure(HttpSecurity http) throws Exception { http .rememberMe() .

当用户在特定页面上执行一些非常敏感的操作时,有没有办法强制使用Spring security重新验证?

使用

SecurityContextHolder.clearContext();


如果您使用的是基于会话的身份验证。为此,您可以使用类似的方法:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .rememberMe()
            .and()
            .authorizeRequests()
                .anyRequest().permitAll()
            .and()
            .formLogin()
                .loginPage("/logout")
                .loginProcessingUrl("/j_spring_security_check")
                .defaultSuccessUrl("/my-profile")
                .usernameParameter("username")
                .passwordParameter("password")
                .failureUrl("/login?error")
            .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login?logout").deleteCookies("JSESSIONID")
                .invalidateHttpSession(true);

}
如果用户执行了不允许的请求,Spring Security会自动重定向到登录页面。这就是为什么要重定向到注销
.loginPage(“/logout”)
,然后在注销
.logoutSuccessUrl(“/login?logout”)
后重定向到登录页面的原因

HttpSession session = request.getSession(false);
if (session != null) {
  session.invalidate();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .rememberMe()
            .and()
            .authorizeRequests()
                .anyRequest().permitAll()
            .and()
            .formLogin()
                .loginPage("/logout")
                .loginProcessingUrl("/j_spring_security_check")
                .defaultSuccessUrl("/my-profile")
                .usernameParameter("username")
                .passwordParameter("password")
                .failureUrl("/login?error")
            .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login?logout").deleteCookies("JSESSIONID")
                .invalidateHttpSession(true);

}