【问题标题】:PreAuthorize("isAuthenticated()") not working on RestControllerPreAuthorize("isAuthenticated()") 在 RestController 上不起作用
【发布时间】:2017-11-16 12:21:51
【问题描述】:

我发现了很多类似的问题,但没有一个能解决我的问题我的问题是: 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");
}

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

【问题讨论】:

  • 是的,我使用 启用它
  • 尝试在value属性中添加它:@PreAuthorize( value="isAuthenticated()")
  • 不行,我也有同样的问题
  • bean 的容器如何发现你的配置?在您的应用程序中,您有一个 servlet 配置(Web 上下文的一部分),并且您有包含持久性配置、安全配置等的应用程序上下文(或根上下文)...
  • 并在我的 applicationContext 中添加以下行:

标签: java json rest spring-security uri


【解决方案1】:

@PreAuthorize 替换为@Secured 并在安全xml 文件中添加secured-annotations="enabled" 后问题已修复。

<global-method-security secured-annotations="enabled"/>

在我的 RestController 上

@Secured

【讨论】:

    【解决方案2】:

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

    @EnableGlobalMethodSecurity(prePostEnabled = true)
    

    感谢这篇文章:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 2021-02-21
      • 2019-05-09
      • 2020-06-24
      • 2015-12-09
      相关资源
      最近更新 更多