【问题标题】:How to debug Spring Security authorization annotations?如何调试 Spring Security 授权注解?
【发布时间】:2015-04-19 03:19:55
【问题描述】:

我有 Spring Security 应用程序,希望在其中启用注释安全性(授权前和授权后)。 我也有一个小的示例应用程序,我已经在其中实现了它。一切正常。 但是将配置移动到主要应用程序失败了。控制台没有错误。但是注释不起作用。看来,它们根本没有被阅读。 所有配置和组件版本完全相同。

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

security-context 和 servlet-context 中的记录。 但是在主应用程序中,@Controller 方法和 @Service 方法都不受注解的保护。

如何调试它?

解决了!

切换到 前/后注释工作正常。

【问题讨论】:

  • 在工作示例中,我看到日志记录:INFO:org.springframework.security.config.method.GlobalMethodSecurityBeanDefinitionParser - 启用了表达式。但在主应用程序中我找不到它
  • sts 用 "5 Spring AOP marker at this line" 行标记

标签: spring spring-security annotations authorization spring-annotations


【解决方案1】:

设置 org.springframework.security 的日志级别为调试。在使用注释调用方法时,您可以找到指示正在应用拦截器的日志消息,尤其是查找: 调试方法安全拦截器

更新: 这意味着您的示例应用程序和主应用程序之间存在一些配置差异 一些需要寻找的指针:

【讨论】:

  • 在示例应用程序中我看到 MethodSecurityInterceptor 和 ExpressionBasedPostInvocationAdvice 。但在主应用程序中没有一个(只有intercept.FilterSecurityInterceptor)。
  • 问题是,如何调试未使用注释的原因。
  • 你是对的!前后注释而不是安全作品。
【解决方案2】:

您可以添加到您的application.yaml:

logging.level.org.springframework.security: DEBUG

或加application.properties:

logging.level.org.springframework.security=DEBUG

或添加到您的WebSecurityConfig 注释EnableWebSecuritydebug = true

@Configuration
@EnableWebSecurity(debug = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  // ...
}

【讨论】:

    【解决方案3】:

    如果您只想知道哪个方法失败了,只需为此异常过滤器设置日志记录级别:

    logging.level.org.springframework.security.web.access.ExceptionTranslationFilter: TRACE
    

    它只会显示失败方法的堆栈跟踪,而不是不必要的垃圾日志;-)

    【讨论】:

    • 这实际上是一个很好的应用程序,在维护良好的应用程序中,我们不想看到超出我们需要的东西,我想知道为什么人们只是在整个安全包上启用调试......但是有只有trace 日志,所以应该是: TRACE
    猜你喜欢
    • 2012-11-21
    • 2013-03-15
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2017-04-20
    • 2020-04-25
    • 2016-10-24
    相关资源
    最近更新 更多