【问题标题】:spring security settings for URL and methodURL和方法的spring安全设置
【发布时间】:2015-10-25 15:20:51
【问题描述】:

我有一个关于 Spring Security 的问题。我有几个页面——A、B 和 C——和 4 个 http 方法来操作它们:GET、PUT、POST、DELETE。对于每个 A+GET 组合,我希望拥有resource-Page-Method 形式的特殊权限。我该如何实施?以下代码不起作用:如果他们没有任何权限,它允许所有用户事件的所有事情。

@Override
protected void configure(HttpSecurity http) throws Exception { 


  RequestHeaderAuthenticationFilter siteMinderFilter = new RequestHeaderAuthenticationFilter();
  siteMinderFilter.setPrincipalRequestHeader("SM_USER");
  siteMinderFilter.setAuthenticationManager(authenticationManager());

  http.addFilter(siteMinderFilter);  


  List<HttpMethod> methods = new ArrayList<HttpMethod>();
  methods.add(HttpMethod.GET);
  methods.add(HttpMethod.POST);
  methods.add(HttpMethod.PUT);
  methods.add(HttpMethod.DELETE);

 List<String> resources = new ArrayList<String>();
 resources.add("A");
 resources.add("B");
 resources.add("C");     
 ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http.authorizeRequests();

 for (HttpMethod method:methods){
     for (String resource: resources){
         String auth = "resource-"+resource+"-"+method.name();
         registry.antMatchers(method, "**/"+resource+"/**")
         .hasAuthority(auth);
     }
 }

  http = registry.and();
  http.formLogin();   

}

【问题讨论】:

  • 你为什么不看我的测试?我写道:对于每个组合,我想添加一个权限。因此,当然,每个用户都应该登录,但不仅如此。我想要一个想要访问页面 A 的 GET 和 PUT 方法以及页面 B 的 DELETE 方法的用户拥有权限:resource-A-GET、resource-A-PUT、resource-B-DELETE
  • 那么对于你的每一种方法,你都需要一个角色,一个用户可以有多个角色。这种思路有什么问题?例如:ROLE_GET、ROLE_DELETE、ROLE_POST 等
  • 你为什么不看我的文字?我想对每个角色和方法的组合都有一个特殊的权限。我想通过某种方式定义我的安全权限,这在春季安全中也是允许的(阅读“hasAuthority()”方法)
  • 对于初学者,我正在阅读您的文字,请在此处阅读答案:stackoverflow.com/questions/19525380/…

标签: java spring spring-security


【解决方案1】:

过滤器不起作用的唯一原因是匹配器模式开头缺少斜线/

不是这个

 registry.antMatchers(method, "**/"+resource+"/**") 

我应该写这个

 registry.antMatchers(method, "/**/"+resource+"/**")

【讨论】:

    猜你喜欢
    • 2015-04-26
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多