【发布时间】:2019-07-22 00:59:12
【问题描述】:
我配置WebSecurityConfig,在内存中创建用户
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("username")
.password(passwordEncoder().encode("password"))
.authorities("READ_ORDERS")
.roles("USER");
}
配置 WebSecurityConfig
@Configuration
@Order(1)
public static class BasicAuthenticationAdapter extends WebSecurityConfigurerAdapter {
private final AuthenticationEntryPoint authEntryPoint;
@Autowired
public BasicAuthenticationAdapter(AuthenticationEntryPoint authEntryPoint) {
this.authEntryPoint = authEntryPoint;
}
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/orders**")
.authorizeRequests()
.anyRequest().hasRole("USER")
.and()
.csrf().disable()
.httpBasic().authenticationEntryPoint(authEntryPoint)
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
当我第一次尝试使用无效凭据进行授权时 - 401 异常,没关系。 但是授权成功后,当我使用无效的用户名和密码时, 我也授权了。 可能是什么问题?
【问题讨论】:
-
您之前的用户还登录吗?你关闭会话了吗?
-
@Stultuske 我只是通过邮递员发送请求顺便说一句,我该如何检查?
-
配置错误。需要在 antMather http .csrf().disable() .antMatcher("/orders/**") .authorizeRequests() .antMatchers("/orders/**").hasRole("READ_ORDERS")
标签: java spring-boot spring-security