【问题标题】:How to allow request from a certain url with spring security如何使用spring security允​​许来自某个url的请求
【发布时间】:2017-09-13 20:21:13
【问题描述】:

我正在开发一个简单的项目,以了解有关 Spring 的一些知识。

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

“保存”按钮发送带有路径 user/signup 的发布请求,但失败,网络控制台显示 302,我认为这是一个安全问题,因为如果我进行身份验证然后我尝试注册用户,则请求成功。 因此,也许我需要对 Spring Security 说,注册请求必须对所有用户可用,也适用于未经身份验证的用户。 我将路径 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)

【问题讨论】:

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


    【解决方案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();
    }
    

    【讨论】:

    • 使用它说的 403
    • 403的请求是什么?
    • 请用/user/signup替换user/signup
    【解决方案2】:

    这个

    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

    【讨论】:

    • 它返回 '403' :(
    • 哪个请求?
    猜你喜欢
    • 2022-06-15
    • 2021-01-04
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2016-09-14
    • 2017-11-06
    • 2020-06-10
    相关资源
    最近更新 更多