开发者社区 问答 正文

spring security很奇怪的问题? 在配置类红使用access表达式一?400报错

.antMatchers("/ll").access("@myServiceImpl.hasPermission(request,authentication)")
    //正常
@PreAuthorize("@myServiceImpl.hasPermission(request,authentication)")
@RequestMapping("/test1")
public String test1(HttpServletRequest request) {
    return "test1";
}//报错

EL1008E: Property or field 'request' cannot be found on object of type 'org.springframework.security.access.expression.method.MethodSecurityExpressionRoot' - maybe not public or not valid?

 

这是判断逻辑;

public boolean hasPermission(HttpServletRequest request, Authentication authentication) {

   Object obj = authentication.getPrincipal();

   if (obj instanceof UserDetails) {
      UserDetails user = (UserDetails) obj;
      Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
      return authorities.contains(new SimpleGrantedAuthority(request.getRequestURI()));
   }
   return false;
}

展开
收起
爱吃鱼的程序员 2020-06-05 15:01:59 1036 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>已解决;需要用#来获取方法参数;如</p> 
    
    @PreAuthorize("@myServiceImpl.hasPermission(#request,#authentication)")
    2020-06-05 15:02:10
    赞同 展开评论