Spring Security中的hasPermission()不会调用CustomPermissionEvaluator

时间:2017-02-02 09:44:21

标签: java spring-security

请参考真正的问题为什么我的@PreAuthorize(“hasPermission(#user,'write')”)无法正常工作

基本上我试图检查普通用户

我的controllerClass

package com.***.appconfig.controller;

import com.***.appconfig.dao.UserDaoImplementation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.***.appconfig.model.User;
import com.***.appconfig.security.CustomPermissionEvaluator;

@Controller
public class CheckPermissionController {
    public static User user = new User();
    UserDaoImplementation userDao = new UserDaoImplementation();
    Boolean directPermission = false;
    CustomPermissionEvaluator customPermissionEvaluator = new CustomPermissionEvaluator();

    @RequestMapping("/checkPermission")
    protected ModelAndView direct() throws Exception {
        System.out.println("in direct");
        user.setUserName("andrew");
        userDao.addListValues(user);
        System.out.println("before assign");
        directPermission = userDao.assignUser(user);
        System.out.print("after assign");
        if (directPermission) {
            return new ModelAndView("checkPermission");
        } else {
            return new ModelAndView("login");
        }
    }
}

这是我的Dao

import com.***.appconfig.model.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import java.util.HashMap;

@Component
public class UserDaoImplementation implements UserDao {

    @Override
    public User addListValues(User user) {
        HashMap < String, String > permissionList = new HashMap < String, String > ();
        permissionList.put("server", "write");
        user.setPermissionList(permissionList);
        return null;
    }

    @PreAuthorize("hasPermission(#user,'write')")
    public Boolean assignUser(User user) {
        System.out.println("in assign");
        return true;
    }
}

这是我的CustomPermissionEvaluator

package com.***.appconfig.security;

import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import com.***.appconfig.controller.CheckPermissionController;
import com.***.appconfig.model.User;
import com.***.appconfig.dao.UserDaoImplementation;
import java.io.Serializable;
import java.util.HashMap;

public class CustomPermissionEvaluator implements PermissionEvaluator {

    public static User user;
    public UserDaoImplementation userDao;

    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
        setPermissions();
        String targetType = targetDomainObject.getClass().getSimpleName().toUpperCase();
        HashMap < String, String > permissionList = user.getPermissionList();
        System.out.print("before check");
        if (permissionList.containsValue("write")) {
            System.out.print("success check");
            hasPermission = true;
        }
        return hasPermission;
    }

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

    public void setPermissions() {
        user.setUserName("andrew");
        userDao.addListValues(user);
    }
}

我创建了一个重复的用户对象,以便在PermissionEvaluator中进行动态填充。不会调用hasPermission()覆盖。

这是我的spring-security.xml

<http auto-config="true">
    <access-denied-handler error-page="/403page" />
    <intercept-url pattern="/user" access="ROLE_USER" />
    <intercept-url pattern="/admin" access="ROLE_ADMIN" />
    <form-login login-page='/login' username-parameter="username" password-parameter="password" default-target-url="/user" authentication-failure-url="/login?authfailed" />
    <logout logout-success-url="/login?logout" />
</http>
<global-method-security pre-post-annotations="enabled" secured-annotations="enabled">
    <expression-handler ref="expressionHandler" />
</global-method-security>
<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password, enabled from users where username=?" authorities-by-username-query="select username, role from user_roles where username =?  " />
    </authentication-provider>
</authentication-manager>
<beans:bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
    <beans:property name="permissionEvaluator" ref="permissionEvaluator" />
</beans:bean>
<beans:bean name="permissionEvaluator" class="com.coolminds.appconfig.security.CustomPermissionEvaluator" />undefined</beans:beans>

1 个答案:

答案 0 :(得分:0)

您的Controller类应该注入所有依赖项,以确保Spring可以创建适当的代理对象:

package com.***.appconfig.controller;

import com.***.appconfig.dao.UserDaoImplementation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.***.appconfig.model.User;

@Inject
UserDao userDao;

@Controller
public class CheckPermissionController {

    @RequestMapping("/checkPermission")
    protected ModelAndView direct() throws Exception {
        User user = new User();
        boolean directPermission = false;

        System.out.println("in direct");
        user.setUserName("andrew");
        userDao.addListValues(user);
        System.out.println("before assign");
        directPermission = userDao.assignUser(user);
        System.out.print("after assign");
        if (directPermission) {
            return new ModelAndView("checkPermission");
        } else {
            return new ModelAndView("login");
        }
    }
}