Commit 63fc2667 by fengshuonan

更改权限校验的类结构

parent cc591770
...@@ -16,12 +16,13 @@ ...@@ -16,12 +16,13 @@
package cn.stylefeng.guns.core.aop; package cn.stylefeng.guns.core.aop;
import cn.stylefeng.guns.core.common.annotion.Permission; import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.shiro.check.PermissionCheckManager; import cn.stylefeng.guns.core.shiro.service.PermissionCheckService;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -39,6 +40,9 @@ import java.lang.reflect.Method; ...@@ -39,6 +40,9 @@ import java.lang.reflect.Method;
@Order(200) @Order(200)
public class PermissionAop { public class PermissionAop {
@Autowired
private PermissionCheckService check;
@Pointcut(value = "@annotation(cn.stylefeng.guns.core.common.annotion.Permission)") @Pointcut(value = "@annotation(cn.stylefeng.guns.core.common.annotion.Permission)")
private void cutPermission() { private void cutPermission() {
...@@ -51,16 +55,19 @@ public class PermissionAop { ...@@ -51,16 +55,19 @@ public class PermissionAop {
Permission permission = method.getAnnotation(Permission.class); Permission permission = method.getAnnotation(Permission.class);
Object[] permissions = permission.value(); Object[] permissions = permission.value();
if (permissions.length == 0) { if (permissions.length == 0) {
//检查全体角色 //检查全体角色
boolean result = PermissionCheckManager.checkAll(); boolean result = check.checkAll();
if (result) { if (result) {
return point.proceed(); return point.proceed();
} else { } else {
throw new NoPermissionException(); throw new NoPermissionException();
} }
} else { } else {
//检查指定角色 //检查指定角色
boolean result = PermissionCheckManager.check(permissions); boolean result = check.check(permissions);
if (result) { if (result) {
return point.proceed(); return point.proceed();
} else { } else {
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
*/ */
package cn.stylefeng.guns.core.shiro; package cn.stylefeng.guns.core.shiro;
import cn.stylefeng.roses.core.util.ToolUtil; import cn.stylefeng.guns.core.shiro.service.UserAuthService;
import cn.stylefeng.guns.core.shiro.factory.IShiro; import cn.stylefeng.guns.core.shiro.service.impl.UserAuthServiceServiceImpl;
import cn.stylefeng.guns.core.shiro.factory.ShiroFactroy;
import cn.stylefeng.guns.modular.system.model.User; import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.AuthenticationToken;
...@@ -42,7 +42,7 @@ public class ShiroDbRealm extends AuthorizingRealm { ...@@ -42,7 +42,7 @@ public class ShiroDbRealm extends AuthorizingRealm {
@Override @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
throws AuthenticationException { throws AuthenticationException {
IShiro shiroFactory = ShiroFactroy.me(); UserAuthService shiroFactory = UserAuthServiceServiceImpl.me();
UsernamePasswordToken token = (UsernamePasswordToken) authcToken; UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
User user = shiroFactory.user(token.getUsername()); User user = shiroFactory.user(token.getUsername());
ShiroUser shiroUser = shiroFactory.shiroUser(user); ShiroUser shiroUser = shiroFactory.shiroUser(user);
...@@ -54,7 +54,7 @@ public class ShiroDbRealm extends AuthorizingRealm { ...@@ -54,7 +54,7 @@ public class ShiroDbRealm extends AuthorizingRealm {
*/ */
@Override @Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
IShiro shiroFactory = ShiroFactroy.me(); UserAuthService shiroFactory = UserAuthServiceServiceImpl.me();
ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal(); ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
List<Integer> roleList = shiroUser.getRoleList(); List<Integer> roleList = shiroUser.getRoleList();
......
/**
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.stylefeng.guns.core.shiro.check;
import cn.stylefeng.roses.core.util.SpringContextHolder;
/**
* 权限检查管理器(入口)
*/
public class PermissionCheckManager {
private final static PermissionCheckManager me = new PermissionCheckManager();
private ICheck defaultCheckFactory = SpringContextHolder.getBean(ICheck.class);
public static PermissionCheckManager me() {
return me;
}
private PermissionCheckManager() {
}
public PermissionCheckManager(ICheck checkFactory) {
this.defaultCheckFactory = checkFactory;
}
public void setDefaultCheckFactory(ICheck defaultCheckFactory) {
this.defaultCheckFactory = defaultCheckFactory;
}
public static boolean check(Object[] permissions) {
return me.defaultCheckFactory.check(permissions);
}
public static boolean checkAll() {
return me.defaultCheckFactory.checkAll();
}
}
/** /**
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,12 +13,12 @@ ...@@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.stylefeng.guns.core.shiro.check; package cn.stylefeng.guns.core.shiro.service;
/** /**
* 检查用接口 * 检查用接口
*/ */
public interface ICheck { public interface PermissionCheckService {
/** /**
* 检查当前登录用户是否拥有指定的角色访问当 * 检查当前登录用户是否拥有指定的角色访问当
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.stylefeng.guns.core.shiro.factory; package cn.stylefeng.guns.core.shiro.service;
import cn.stylefeng.guns.core.shiro.ShiroUser; import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.model.User; import cn.stylefeng.guns.modular.system.model.User;
...@@ -27,7 +27,7 @@ import java.util.List; ...@@ -27,7 +27,7 @@ import java.util.List;
* @author fengshuonan * @author fengshuonan
* @date 2016年12月5日 上午10:23:34 * @date 2016年12月5日 上午10:23:34
*/ */
public interface IShiro { public interface UserAuthService {
/** /**
* 根据账号获取登录用户 * 根据账号获取登录用户
......
/** /**
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,15 +13,14 @@ ...@@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.stylefeng.guns.core.shiro.check; package cn.stylefeng.guns.core.shiro.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.core.listener.ConfigListener;
import cn.stylefeng.guns.core.shiro.ShiroKit; import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser; import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.shiro.service.PermissionCheckService;
import cn.stylefeng.roses.core.util.HttpContext; import cn.stylefeng.roses.core.util.HttpContext;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.stylefeng.guns.core.listener.ConfigListener;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -32,13 +31,8 @@ import java.util.ArrayList; ...@@ -32,13 +31,8 @@ import java.util.ArrayList;
* 权限自定义检查 * 权限自定义检查
*/ */
@Service @Service
@DependsOn("springContextHolder")
@Transactional(readOnly = true) @Transactional(readOnly = true)
public class PermissionCheckFactory implements ICheck { public class PermissionCheckServiceServiceImpl implements PermissionCheckService {
public static ICheck me() {
return SpringContextHolder.getBean(ICheck.class);
}
@Override @Override
public boolean check(Object[] permissions) { public boolean check(Object[] permissions) {
......
...@@ -13,16 +13,17 @@ ...@@ -13,16 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.stylefeng.guns.core.shiro.factory; package cn.stylefeng.guns.core.shiro.service.impl;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory; import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.constant.state.ManagerStatus; import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.shiro.service.UserAuthService;
import cn.stylefeng.guns.modular.system.dao.MenuMapper; import cn.stylefeng.guns.modular.system.dao.MenuMapper;
import cn.stylefeng.guns.modular.system.dao.UserMapper; import cn.stylefeng.guns.modular.system.dao.UserMapper;
import cn.stylefeng.guns.modular.system.model.User; import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import org.apache.shiro.authc.CredentialsException; import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.SimpleAuthenticationInfo;
...@@ -39,7 +40,7 @@ import java.util.List; ...@@ -39,7 +40,7 @@ import java.util.List;
@Service @Service
@DependsOn("springContextHolder") @DependsOn("springContextHolder")
@Transactional(readOnly = true) @Transactional(readOnly = true)
public class ShiroFactroy implements IShiro { public class UserAuthServiceServiceImpl implements UserAuthService {
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;
...@@ -47,8 +48,8 @@ public class ShiroFactroy implements IShiro { ...@@ -47,8 +48,8 @@ public class ShiroFactroy implements IShiro {
@Autowired @Autowired
private MenuMapper menuMapper; private MenuMapper menuMapper;
public static IShiro me() { public static UserAuthService me() {
return SpringContextHolder.getBean(IShiro.class); return SpringContextHolder.getBean(UserAuthService.class);
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment