自定义身份验证 - Spring 引导 403 禁止错误
Custom Authentication - Spring boot 403 forbidden error
我正在尝试实现自定义身份验证,身份验证工作正常,但授权有问题。我正在使用 JWT 令牌,任何 API 我尝试访问它都会抛出 403 禁止错误。我不确定哪里出了问题。我在 github 中有完整的源代码。 https://github.com/vivdso/SpringAuthentication、Spring boot magic 对此无效。任何指针都表示赞赏。
使用 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();
}
我没有任何自定义的授权实现。
问题已解决,我已更新 Github 存储库。 spring 启动安全性工作正常,问题是分配给用户集合的角色是一个 Json 字符串对象(例如 {"role":"ROLE_ADMIN"})而不是 sting对象 "ROLE_ADMIN".
谢谢
我正在尝试实现自定义身份验证,身份验证工作正常,但授权有问题。我正在使用 JWT 令牌,任何 API 我尝试访问它都会抛出 403 禁止错误。我不确定哪里出了问题。我在 github 中有完整的源代码。 https://github.com/vivdso/SpringAuthentication、Spring boot magic 对此无效。任何指针都表示赞赏。
使用 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();
}
我没有任何自定义的授权实现。
问题已解决,我已更新 Github 存储库。 spring 启动安全性工作正常,问题是分配给用户集合的角色是一个 Json 字符串对象(例如 {"role":"ROLE_ADMIN"})而不是 sting对象 "ROLE_ADMIN".
谢谢