春季安全性@PreAuthorize NullPointerException。为什么?

时间:2020-03-25 16:54:56

标签: java spring spring-security nullpointerexception

我正在尝试检查用户在控制器中的角色,因此当用户调用特定的Web地址时,他可以访问该页面(或不可以)。

因此,我将 @PreAuthorize(“ hasPermission ...)放入控制器的一种方法中,并创建了我的自定义* PermissionEvaluator *。它使用两个字符串作为参数(实体名称-字符串,权限名称-字符串),我将稍后从用户的角色对象中获取。出于测试目的,它始终返回true。

问题:放置@PreAuthorize时,我始终会收到NullPointerException。你能解释我在做什么错吗?

控制器

@RequestMapping(value = "goal/new", method = RequestMethod.GET)
@PreAuthorize("hasPermission('GOAL', 'WRITE')")
public String add (Model model,  RedirectAttributes redirect) {
   User user = AuthUtils.getCurrentUser();
   Goal goal = new Goal();
   Set<Unit> units = unitService.getUnitsByRole(user.getRoles());
   model.addAttribute("goal", goal);
   model.addAttribute("units", units);
return WEB_FORM_URL;
}

权限评估器

@Component
public class CustomPermissionEvaluator implements PermissionEvaluator {
    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
        System.out.println("Permission eveluator: called");
        boolean permissionGranted = true;
        return permissionGranted;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable serializable, String targetType,
                                 Object permission) {
        return false;
    }
}

GlobalMethodSecurityConfiguration

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true, proxyTargetClass = true)
public class CustomMethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    @Autowired
    CustomPermissionEvaluator permissionEvaluator;

    @Bean
    public MethodSecurityExpressionHandler methodSecurityExpressionHandler() {
        DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();
        handler.setPermissionEvaluator(permissionEvaluator);
        return handler;
    }
}

错误堆栈

java.lang.NullPointerException: null
    at org.springframework.security.access.expression.SecurityExpressionRoot.hasPermission(SecurityExpressionRoot.java:177) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
    at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:130) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:138) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:94) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:114) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:300) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:26) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.security.access.expression.method.ExpressionBasedPreInvocationAdvice.before(ExpressionBasedPreInvocationAdvice.java:59) ~[spring-security-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]

1 个答案:

答案 0 :(得分:0)

我不确定100%,但是可以将CustomMethodSecurityConfig中的方法签名更改为实际上覆盖GlobalMethodSecurityConfiguration中的适当方法。

所以

 @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {

代替

  @Bean
    public MethodSecurityExpressionHandler methodSecurityExpressionHandler() {
相关问题