当前位置: 首页 > 知识库问答 >
问题:

带有匿名用户的Spring Security性和RequestCache

羊舌和安
2023-03-14

这是我的安全配置

@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,因此在登录后,SavedRequestStawareAuthenticationSuccessHandler不会重定向到请求的页面。

我认为这是因为permitAll创建了一个匿名用户。

我如何配置Spring Security才能使SavedRequestAwareAuthenticationSuccessHandler工作?

共有1个答案

皇甫伟彦
2023-03-14

作为答案发布,以便我可以包含我的代码。我尝试重新创建您的设置,这是我的安全配置

@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();
    }
}

和我的控制器

@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>";
    }
}

我可以打开它,导航到< code>/,单击< code>/test的链接,这时我会被重定向到登录表单。登录后,我被重定向到< code>/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);
    }
}
 类似资料:
  • 我用Spring Security和OAuth2保护了我的Spring REST API,我可以成功地检索令牌并访问我的API。我的应用程序自定义了OAuth2客户端。 当我添加第二个antmatcher/anonymous时,它就无法工作,而且它也没有真正表达我的意图--例如,我不想对GETs进行匿名访问,而是在POSTs上进行身份验证(使用很容易做到)。 如何使OAuth2身份验证可选?

  • 问题内容: 我想创建一个将包含变量的脚本,并且 仅 当尚不存在这种登录名 时才在Postgres数据库中创建用户。我以为这会起作用,但是我不能说出问题所在: __ 如何强制用变量内容替换变量? 而且和之间有什么区别? 问题答案: 要参数化标识符或语法元素,通常需要将动态SQL与-最好结合使用,以易于使用。 但实用的命令(包括所有SQL DDL语句)不允许值或参数替换的传递 在所有 。您需要先连接完

  • 我们使用的是带有Spring Security性的Spring MVC。 该网站应可供匿名用户使用,包括首选项设置(例如打开或关闭页面上的筛选器)。 如果匿名用户在第二天返回(在新会话中),应记住这些首选项。 匿名用户可以随时选择注册配置文件(用户/密码组合),以前匿名用户设置的所有首选项都应存储在新的配置文件中。 或者,匿名用户可以选择使用已注册的配置文件登录,并且应该向他们提供将匿名设置的首选

  • 一个元素可以按照HTML元素的嵌套方式包含其他元素,大多数情况下,框都是由显式定义的元素所生成的。 然而,当把文本直接添加到一个块容器元素中(不是包含在行内元素)时,即便没有为这些文本显式定义元素,它们也会生成框。 没有被元素显式包含的文本,称作匿名文本,因为没有与之关联的元素。把匿名文本所生成框,称作匿名框。匿名框分为两种,一种是匿名块框,另一种是匿名行内框。 当把文本直接添加到一个块容器元素中

  • 我有一个用Keycloak保护的JEE服务(JaxRx),身份验证是有效的,但是当我想用应用安全性时,我得到了。 该服务部署在Wildfly11中,关于文档,我使用和下一个配置文件()将安全上下文传播到EJB层。 我在Wildfly中安装了keycloak。 如图中所示,请求用户是在RequestContext中标识的,但在SessionContext(上下文)不存在的情况下,它显示而不是用户。

  • 这样,把pdf文件复制给别人就会迫使“复印机”说出他的用户名和密码…… 编辑:密码是打开文件而不是编辑或打印它,我希望用户能够突出显示文本和添加他们自己的注释