如何允许来自具有弹簧安全性的特定URL的请求

时间:2017-09-13 20:21:14

标签: java spring spring-mvc spring-boot spring-security

我正在开发一个简单的项目,以便了解春天。

我已经创建了登录页面和所有身份验证管理,现在我正在尝试构建注册页面,我已经完成了后端和前端,但我遇到了问题。

按钮'保存'发送带有路径 user / signup 的帖子请求但是它失败了,网络控制台说 302 ,我认为这是一个安全问题,因为如果我进行身份验证然后我尝试注册用户请求成功。 因此,我可能需要说明一下安全性,注册请求必须对所有用户可用,对于未经身份验证的用户也是如此。 我把路径 user / signup 放在spring boot中,但它不起作用,我也试过只使用 / signup

@Override
    public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers("/script/**", "/css/**", "/getRegisterPage","user/signup");
    }

这是项目:https://github.com/StefanoPisano/expenses(分支0.2)

2 个答案:

答案 0 :(得分:1)

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/script/**", "/css/**", "/getRegisterPage","/user/signup").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/getLoginPage")
            .loginProcessingUrl("/login")
            .usernameParameter("username")
            .passwordParameter("password")
            .defaultSuccessUrl("/home", true)
    .permitAll();
}

答案 1 :(得分:1)

这个

web.ignoring().antMatchers("/script/**", "/css/**", "/getRegisterPage","/user/signup");

将忽略具有这些模式的所有请求。

您需要的是:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/resources/**", "/user/signup").permitAll()
            .anyRequest().fullyAuthenticated()
            .and()
        .formLogin()
            .loginPage("/getLoginPage")
            .loginProcessingUrl("/login")
            .usernameParameter("username")
            .passwordParameter("password")
            .defaultSuccessUrl("/home", true)
            .permitAll();

    http
        .csrf().disable();
}

permitAll() - 允许任何人(包括未经身份验证的用户)访问该URL。 有关详细信息,请查看此https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html

相关问题