[英]@PreAuthorize(“isAuthenticated()”) redirected server too many times
I'm trying to learn the @EnableGlobalMethodSecurity annotation in Spring. 我正在尝试在Spring中学习@EnableGlobalMethodSecurity批注。 Here is an example that doesnt seem to work for me.
这是一个对我似乎不起作用的示例。
THIS WORKS: Let's say I have a global controller that shows the logged-in username on every page, like so: 这项工作:假设我有一个全局控制器,该控制器在每个页面上显示登录的用户名,如下所示:
@ControllerAdvice
public class GlobalController {
@ModelAttribute("username")
public String getCurrUser(Principal principal) {
if (principal != null) {
return principal.getName();
}
return null;
}
}
THIS DOESNT WORK: The same controller now, but this time with preAuthorize annotation. 这项工作:现在是相同的控制器,但是这次带有preAuthorize注释。 This returns me the following error message: localhost redirected you too many times.
这将向我返回以下错误消息: localhost将您重定向了太多次。
@ControllerAdvice
public class GlobalController {
@PreAuthorize("isAuthenticated()")
@ModelAttribute("username")
public String getCurrUser(Principal principal) {
return principal.getName();
}
}
My SecurityConfig class is annotated like this: 我的SecurityConfig类的注释如下:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
So how can I solve this error message with PreAuthorize annotation? 那么,如何使用PreAuthorize注释解决此错误消息?
I recently have been learning Spring and Spring security too. 我最近也一直在学习Spring和Spring安全性。 Doe your base url pattern has restriction or not ?
您的基本网址格式有限制吗? I tried to redirect all the logins to the same a point which according to my configuration required authentication.
我尝试将所有登录名重定向到同一点,根据我的配置需要进行身份验证。
See if the configuration is correct and access security is properly set on the code. 查看配置是否正确,并且在代码上正确设置了访问安全性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.