gpt4 book ai didi

java - PreAuthorize ("isAuthenticated()") 不适用于 RestController

转载 作者:行者123 更新时间:2023-11-29 06:52:08 24 4
gpt4 key购买 nike

我发现了很多类似的问题,但都没有解决我的问题我的问题是:PreAuthorize("isAuthenticated()") 在我的 RestController 上不起作用。

我的配置安全性是:

<global-method-security pre-post-annotations="enabled"/>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<password-encoder ref="passwordEncoder" />
<jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="
select login,password,1
from test tst where tst.login=?"
authorities-by-username-query="
select login,'ROLE_SAVE' from test tst where tst.login=?"
/>
</authentication-provider>
</authentication-manager>

在我的 RestController 上我添加了这个注解:@PreAuthorize("isAuthenticated()")

@RestController
@PreAuthorize("isAuthenticated()")
@RequestMapping("/api/test")
public class PrinterController{

@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseStatus test() {
System.out.println("test");
}

但不工作任何用户都可以使用此资源。

最佳答案

您需要将以下注释添加到您的安全配置类:

@EnableGlobalMethodSecurity(prePostEnabled = true)

感谢这篇文章:

https://nixmash.com/post/spring-mvc-method-security-with-preauthorize-and-sp-el

关于java - PreAuthorize ("isAuthenticated()") 不适用于 RestController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44543236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com