如何在Spring安全性中解决IllegalArgumentException

时间:2016-04-08 01:10:53

标签: spring-security

我使用的是弹簧安全装置。当我浏览http://localhost:8080/rest/user/json/quypham时,出现以下错误:

  

[警告] / rest / user / json / quypham java.lang.IllegalArgumentException:   无法评估表达式' ROLE_ADMIN'           在org.springframework.security.access.expression.ExpressionUtils.evalua   teAsBoolean(ExpressionUtils.java:15)           在org.springframework.security.web.access.expression.WebExpressionVoter   .vote(WebExpressionVoter.java:36)           在org.springframework.security.web.access.expression.WebExpressionVoter   .vote(WebExpressionVoter.java:18)           在org.springframework.security.access.vote.AffirmativeBased.decide(Affi)   rmativeBased.java:62)           在org.springframework.security.access.intercept.AbstractSecurityInterce   ptor.beforeInvocation(AbstractSecurityInterceptor.java:232)           在org.springframework.security.web.access.intercept.FilterSecurityInter

     

引起:   org.springframework.expression.spel.SpelEvaluationException:EL1008E:   (位置0):属性或字段' ROLE_ADMIN'无法找到对象   类型' org.s   pringframework.security.web.access.expression.WebSecurityExpressionRoot'    - 也许不公开?           在org.springframework.expression.spel.ast.PropertyOrFieldReference.read   属性(PropertyOrFieldReference.java:215)           在org.springframework.expression.spel.ast.PropertyOrFieldReference.getV   alueInternal(PropertyOrFieldReference.java:85)           在org.springframework.expression.spel.ast.PropertyOrFieldReference.getV   alueInternal(PropertyOrFieldReference.java:78)           在org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(Sp   elNodeImpl.java:114)           在org.springframework.expression.spel.standard.SpelExpression.getValue(   SpelExpression.java:105)           在org.springframework.security.access.expression.ExpressionUtils.evalua

我认为我的userservice-servlet.xml文件中出现了问题:

<security:http>
  <security:http-basic/>
  <security:intercept-url pattern="/rest/**" access="ROLE_ADMIN"/>      
</security:http>

<security:authentication-manager alias="authenticationManager">
  <security:authentication-provider>
    <security:user-service>
      <security:user name="datpham" password="Dat12345" authorities="ROLE_ADMIN"/>
      <security:user name="hoaipham" password="Hoai12345" authorities="ROLE_USER"/>
    </security:user-service>
  </security:authentication-provider>
</security:authentication-manager>

以下是UserRestServiceController.java

@Controller
public class UserRestServiceController {
    @Autowired
    public  UserDao userDao;
    @Autowired
    public View jsonTemplate;
    @RequestMapping(value="/rest/user/json/{username}",
            produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.GET)
    public @ResponseBody User loadUser(@PathVariable("username") String name){
        return userDao.loadUser(name);
    }

2 个答案:

答案 0 :(得分:2)

默认情况下,expression-based access-control已启用。这意味着您应该将access值更改为表达式:

<security:intercept-url pattern="/rest/**" access="hasRole('ADMIN')"/> 

作为选项,您可以禁用表达式:

<security:http use-expressions="false">

答案 1 :(得分:0)

对于所有角色,您必须使用'hasRole'。 在你的情况下 访问=“hasRole('ADMIN')” ,当&lt; http use-expressions =“false”&gt; <时使用< / EM>