核心控制包
package io.github.wx.core.config;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.github.jieblog.plugin.shiro.core.ShiroInterceptor;
import com.github.jieblog.plugin.shiro.core.ShiroKit;
import com.github.jieblog.plugin.shiro.core.ShiroPlugin;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import io.github.wx.common.directive.UserCountTag;
import io.github.wx.common.druid.DruidStatViewAuthImpl;
import io.github.wx.core.route.AdminRouts;
import io.github.wx.core.route.BizRoutes;
import io.github.wx.dao.model._MappingKit;
/**
* Created by jie on 2017/4/1.
* 框架的核心配置
*/
public class LmsCoreConfig extends JFinalConfig {
private static Prop p = loadConfig();
private WallFilter wallFilter;
private Engine ms = null;
private static Prop loadConfig() {
try {
return PropKit.use("profile.dev.properties");
} catch (Exception e) {
return PropKit.use("profile.sit.properties");
}
}
/**
* 配置全局常量
*
* @param me Constants
*/
public void configConstant(Constants me) {
me.setDevMode(p.getBoolean("devMode", false));
me.setJsonFactory(new FastJsonFactory());
me.setBaseUploadPath(p.get("uploadPath"));
}
/**
* 配置路由
*
* @param me Routes
*/
public void configRoute(Routes me) {
me.add(new BizRoutes());
me.add(new AdminRouts());
}
/**
* 配置模板引擎
*
* @param me Engine
*/
public void configEngine(Engine me) {
//如果公共模板中有引用shiro标签,那么这个要在它之前
ShiroKit.initDirective(me);
this.ms = me;
}
/**
* 配置插件
*
* @param me Plugins
*/
public void configPlugin(Plugins me) {
RedisPlugin redisPlugin = new RedisPlugin(p.get("redisCacheName"), p.get("redisHost"), p.get("redisPwd"));
me.add(redisPlugin);
ShiroPlugin shiroPlugin = new ShiroPlugin();
shiroPlugin.setLoginUrl("/login");
shiroPlugin.setUnauthorizedUrl("/unauthorized");
me.add(shiroPlugin);
DruidPlugin druidPlugin = getDruidPlugin();
wallFilter = new WallFilter();
wallFilter.setDbType("mysql");
druidPlugin.addFilter(wallFilter);
druidPlugin.addFilter(new StatFilter());
me.add(druidPlugin);
ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
arp.setShowSql(p.getBoolean("devMode", false));
_MappingKit.mapping(arp);
me.add(arp);
}
/**
* 创建DruidPlugin 抽成公告方法,方便生成model时候调用
*
* @return DruidPlugin
*/
static DruidPlugin getDruidPlugin() {
return new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password").trim());
}
/**
* 配置拦截器
*
* @param me Interceptors
*/
public void configInterceptor(Interceptors me) {
me.add(new ShiroInterceptor());
}
/**
* 配置自定义处理器
*
* @param me Handlers
*/
public void configHandler(Handlers me) {
me.add(new DruidStatViewHandler("druid", new DruidStatViewAuthImpl()));
me.add(new ContextPathHandler("ctx"));
}
/**
* 启动后回调
*/
public void afterJFinalStart() {
wallFilter.getConfig().setSelectUnionCheck(false);
ms.addDirective("userCount", UserCountTag.class);
ms.addSharedFunction("_common/common.html");
}
/**
* 程序启动入口
*
* IDEA 启动设置
* JFinal.start("lms-web/src/main/webapp", 80, "/");
* eclipse 启动设置 (/开头)
* JFinal.start("/C:/workspace/LMS/lms-web/src/main/webapp/lms-web/src/main/webapp", 80, "/",10);
*
* @param args Args
*/
public static void main(String[] args) {
JFinal.start("lms-web/src/main/webapp", 80, "/");
}
}
shiro控制包
package io.github.wx.core.config;
import com.jfinal.aop.Duang;
import com.xiaoleilu.hutool.util.CollectionUtil;
import io.github.wx.common.constant.EnumFuctionStatus;
import io.github.wx.dao.model.SysMenu;
import io.github.wx.dao.model.SysRole;
import io.github.wx.dao.model.SysUser;
import io.github.wx.service.MenuService;
import io.github.wx.service.RoleService;
import io.github.wx.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jie on 2017/4/3.
* 自定义shiro Realm
*/
public class ShiroDbRealm extends AuthorizingRealm {
static final UserService userService = Duang.duang(UserService.class);
static final RoleService roleService = Duang.duang(RoleService.class);
static final MenuService menuService = Duang.duang(MenuService.class);
/**
* 认证回调函数,登录时调用.
*
* @param authcToken 登录信息集合
* @return AuthenticationInfo
* @throws AuthenticationException 认证异常
*/
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
SysUser user = new SysUser();
user.setUsername(token.getUsername());
List list = userService.getUserByUsername(user);
// 账号不存在
if (CollectionUtil.isEmpty(list))
throw new UnknownAccountException();//没找到帐号
SysUser sysUser = list.get(0);
if (EnumFuctionStatus.BAD_STATUS.getCode().equals(sysUser.getDelFlag()))
throw new LockedAccountException();
return new SimpleAuthenticationInfo(sysUser.getUsername(),
sysUser.getPassword(), ByteSource.Util.bytes(sysUser.getSalt()), getName());
}
/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
*
* @param principals 授权信息
* @return AuthorizationInfo
*/
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
String loginName = (String) principals.fromRealm(getName()).iterator()
.next();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
SysRole sysRole = roleService.findUserRole(loginName);
if (null == sysRole) {
return info;
}
info.addRole(sysRole.getRoleName());
List menuList = menuService.findByRoleId(sysRole.getRoleId());
List lists = new ArrayList();
for (SysMenu menu : menuList) {
lists.add(menu.getPermission());
}
info.addStringPermissions(lists);
return info;
}
/**
* 更新用户授权信息缓存.
*
* @param principal 用户标志
*/
public void clearCachedAuthorizationInfo(String principal) {
SimplePrincipalCollection principals = new SimplePrincipalCollection(
principal, getName());
clearCachedAuthorizationInfo(principals);
}
/**
* 清除所有用户授权信息缓存.
*/
public void clearAllCachedAuthorizationInfo() {
Cache
shiro配置
[main]
#credentialsMatcher
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=2
#realm
lmsRealm = io.github.wx.core.config.ShiroDbRealm
lmsRealm.credentialsMatcher=$credentialsMatcher
securityManager.realm = $lmsRealm
redisSessionDAO = io.github.wx.common.shiro.cache.RedisSessionDAO
redisSessionDAO.expire = 1800
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
sessionManager.sessionDAO = $redisSessionDAO
securityManager.sessionManager = $sessionManager
cacheManager = io.github.wx.common.shiro.cache.RedisCacheManager
cacheManager.expire = 1800
securityManager.cacheManager = $cacheManager
#cookie
rememberMeCookie = org.apache.shiro.web.servlet.SimpleCookie
rememberMeCookie.name = rememberMe
rememberMeCookie.httpOnly = true
rememberMeCookie.maxAge = 2592000
rememberMeManager = org.apache.shiro.web.mgt.CookieRememberMeManager
rememberMeManager.cookie = $rememberMeCookie
securityManager.rememberMeManager = $rememberMeManager
#这里的规则,web.xml中的配置的ShiroFilter会使用到。
shiro.loginUrl = /login/
[urls]
/userLogin = anon
/static/**=anon
/** = user
登录页面
系统页面
添加用户
退出系统功能
菜单管理
分配角色权限
源码下载地址:https://download.csdn.net/download/u012374381/10548199
如有疑问或者 建议及 请提出留言 谢谢反馈