【问题标题】:Custom Authentication - Spring boot 403 forbidden error自定义身份验证 - Spring Boot 403 禁止错误
【发布时间】:2017-02-22 17:34:02
【问题描述】:

我正在尝试实现自定义身份验证,身份验证工作正常,但授权有问题。我正在使用 JWT 令牌,我尝试访问它的任何 API 都会抛出 403 禁止错误。我不确定出了什么问题。我在 github 上有完整的源代码。 https://github.com/vivdso/SpringAuthentication,Spring Boot 魔法对此不起作用。任何指针都会被察觉。 使用 MongoDb 作为我的存储库来存储用户帐户和角色。
InMemory 身份验证工作正常,但自定义身份验证总是返回 403,下面是我扩展的 WebSecurityConfigurerAdapter

@Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
//        authenticationManagerBuilder.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
//        authenticationManagerBuilder.inMemoryAuthentication().withUser("user").password("user").roles("USER");

authenticationManagerBuilder.authenticationProvider(getCustomAuthenticationProvider()); }

@Override
protected void configure(HttpSecurity http) throws Exception {
 http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/customer").hasAuthority("ADMIN")
                .antMatchers(HttpMethod.GET, "/order").hasAuthority("USER").and()
                .csrf().disable();
    }

@Bean
    protected CustomAuthenticationProvider getCustomAuthenticationProvider(){
        return new CustomAuthenticationProvider();
    }

我没有任何自定义的授权实现。

【问题讨论】:

    标签: mongodb authentication spring-boot jwt http-status-code-403


    【解决方案1】:

    问题已解决,我已更新 Github 存储库。 Spring Boot 安全工作正常,问题是分配给用户集合的角色是 Json 字符串对象(例如 {"role":"ROLE_ADMIN"})而不是 sting 对象 "ROLE_ADMIN"。

    谢谢

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2015-12-02
      • 2017-03-17
      • 2021-04-21
      • 2019-09-22
      • 2015-09-05
      • 1970-01-01
      • 2019-05-12
      • 2019-10-24
      相关资源
      最近更新 更多