java - 尽管允许,但未经授权的请求

标签 java spring spring-boot

我有以下 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/

相关文章:

java - 添加到 ModelAndView 的变量似乎消失了

java - Spring Boot aws 部署问题

Java Jsoup无法选择表

java - 在打印期间跳过 ArrayList 的特定值

java - 如何在 Neo4j Embedded 中查找具有标签子集的节点

java - Hibernate使用@Transactional创建过多的连接,如何防止这种情况?

具有多个作业和 xml 文件的 Spring Batch

java - 使用 Spring Boot 读取资源文件夹中可用的文件

hibernate - 如何将持久性单元的 JPA EntityManagerFactory 设置为不为 'default'

java - 传递的字符串不被视为字符串