簡體   English   中英

Spring 匿名用戶的安全和 RequestCache

[英]Spring Security and RequestCache with Anonymous User

那是我的安全配置

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .requestCache().requestCache(new CustomRequestCache())
            .and().authorizeRequests()
            .antMatchers("/").permitAll()
            .anyRequest().hasAnyAuthority(Role.getAllRoles())
            .and().formLogin().loginPage(LOGIN_URL).permitAll()
                  .loginProcessingUrl(LOGIN_PROCESSING_URL)
            .failureUrl(LOGIN_FAILURE_URL)
            .successHandler(new SavedRequestAwareAuthenticationSuccessHandler())
            .and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL);
}

問題是當我從/導航到受保護的 URL 時不會調用 CustomRequestCache,因此 SavedRequestAwareAuthenticationSuccessHandler 在登錄后不會重定向到請求的頁面。

我假設這是因為 permitAll 創建了一個匿名用戶。

我必須如何配置 Spring 安全性才能使 SavedRequestAwareAuthenticationSuccessHandler 工作?

作為答案發布,以便我可以包含我的代碼。 我嘗試重新創建您的設置,這是我的安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String THE_AUTHORITY = "ROLE_ONE";

    @Override
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password(passwordEncoder().encode("user")).authorities(THE_AUTHORITY);
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requestCache().requestCache(new CustomRequestCache())
                .and().authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().hasAnyAuthority(THE_AUTHORITY)
                .and().formLogin().loginPage("/login").permitAll()
                    .loginProcessingUrl("/login")
                    .failureUrl("/")
                .successHandler(new SavedRequestAwareAuthenticationSuccessHandler())
                .and().logout().logoutSuccessUrl("/");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

和我的 controller

@RestController
public class TestController {

    @GetMapping("/test")
    public String getTest() {
        return "You did it!";
    }

    @GetMapping("/")
    public String getRoot() {
        return "<a href=/test>Go to test</a>";
    }

    @GetMapping("/login")
    public String getLogin() {
        return "<form action=/login method=POST><input name=username /><input name=password /><input type=submit /></form>";
    }
}

我可以打開它,導航到/ ,單擊指向/test的鏈接,此時我被重定向到登錄表單。 登錄后,我被重定向到/test

這是我的 CustomReuqestCache:

public class CustomRequestCache extends HttpSessionRequestCache {
    @Override
    public void saveRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        System.out.println("Saving request to " + httpServletRequest.getRequestURI());
        super.saveRequest(httpServletRequest, httpServletResponse);
    }

    @Override
    public HttpServletRequest getMatchingRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
         System.out.println("Returning request for " + httpServletRequest.getRequestURI());
         return super.getMatchingRequest(httpServletRequest, httpServletResponse);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM