目录
一.spring security控制权限
security的核心功能就是控制权限。这里,我们通过以下几个方面来说明:
(1)数据库动态配置权限
(2)前端页面控制(控制按钮或者菜单是否显示)
(3)后端验证(验证某一个请求是否合法)
二:后端验证
1.验证流程
2.配置
(1)配置自定义角色权限验证
在security中,权限的验证是由PermissionEvaluator来实现的,这里我们需要自定义并集成
package com.config.Seurity.permission;
import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import com.config.Seurity.util.UserUtil;
import com.moudle.system.model.Menu;
import com.moudle.system.service.MenuService;
/**
*
* ClassName: 自定义URL拦截
* Function: 一句话描述功能.
* auth: monxz
* date: 2019年8月29日 下午7:23:44
*
*
*/
@Component
public class CustomPermissionEvaluator implements PermissionEvaluator{
@Autowired
private MenuService menuService;
/**
* 我的url和权限全部放在菜单表中了
* TODO 注意这里,我们这里直接加载菜单中URL以及权限,是应为在MenuService中自动过滤掉了相应权限的路径了
* @see
*/
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
if(UserUtil.currentUser().getRole().getRoleRange() == 0) {
return true;
}
List<Menu> menuList=menuService.findList(new Menu());
for(Menu menu:menuList) {
// if(targetDomainObject.equals(menu.getUrl()) && permission.equals(menu.getParams())) {
// return true;
// }
if( permission.equals(menu.getParams())) {
return true;
}
}
return false;
}
@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType,
Object permission) {
// TODO Auto-generated method stub
return false;
}
}
(2)在config中声明改配置
package com.config.Seurity.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import com.config.Seurity.hander.AjaxSuccessHander;
import com.config.Seurity.hander.AjaxfailHander;
import com.config.Seurity.permission.CustomPermissionEvaluator;
import com.config.Seurity.pwdEnder.MyPasswordEncoder;
import com.config.Seurity.service.LoginService;
import lombok.extern.slf4j.Slf4j;
/**
* spring security的配置
* ClassName: SecurityConfig
* Function: 一句话描述功能.
* auth: monxz
* date: 2019年8月28日 上午10:04:50
*
*
*/
@Configuration
@EnableWebS