我有以下 websecurityconfig 并希望允许任何人访问登录页面,但当我尝试登录时,它会抛出未经授权的异常。当我尝试发送/user/create 请求时,它工作得很好并且完成了它的工作。
http.httpBasic().disable()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/actuator/**").hasAuthority(Role.ADMIN.toString())
.antMatchers("/").permitAll()
.antMatchers(HttpMethod.DELETE,"/user/remove").hasAnyAuthority(Role.ADMIN.toString(), Role.USER.toString())
.antMatchers(HttpMethod.PUT, "/user/create").permitAll()
.antMatchers(HttpMethod.POST, "**/login", "login", "/login").permitAll() // trying combinations of login path
.antMatchers("/admin/**").hasAuthority(Role.ADMIN.toString())
.and()
.apply(new JwtConfig(jwtTokenProvider));
这是登录 Controller 。我将调试器放在返回行中,并且执行事件不会到达该点。安全性抛出异常。
@RestController
@RequestMapping // I also tried @RequestMapping({"","/"})
public class PublicController {
@Autowired
AuthenticationService authenticationService;
@PostMapping(value = "login", consumes = "application/json")
public GenericResponse login(@RequestBody UserLoginDTO userLoginDTO) {
return authenticationService.login(userLoginDTO); // never reached this line
}
}
我该如何处理这个问题?
最佳答案
与其在 HTTP POST 上使用复杂的 antMatcher,为什么不这样做:
.antMatchers("/login*").permitAll()
您还需要能够通过 GET 访问您的登录页面,并且这个简单的映射将适用于您的所有登录情况。
关于java - 尽管允许,但未经授权的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60834713/