简单的springboot shiro nutz 整合
该实例,实现了shiro授权和认证,email邮件发送,前后端代码分离。
email发送,用在用户注册后,进行激活用户账号使用,未激活账户不能登录。
创建数据库:
/*
Navicat MySQL Data Transfer
Source Server : 127.0.0.1
Source Server Version : 50719
Source Host : localhost:3306
Source Database : somuns
Target Server Type : MYSQL
Target Server Version : 50719
File Encoding : 65001
Date: 2018-01-26 10:01:41
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for sys_permissions
-- ----------------------------
DROP TABLE IF EXISTS `sys_permissions`;
CREATE TABLE `sys_permissions` (
`spid` int(32) NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL COMMENT '菜单名称',
`resource_type` varchar(128) DEFAULT NULL COMMENT '资源类型',
`permission` varchar(128) DEFAULT NULL COMMENT '权限字符串',
`parent_id` bigint(64) DEFAULT NULL COMMENT '父编号',
`available` tinyint(1) DEFAULT NULL COMMENT '可用',
`url` varchar(128) DEFAULT NULL COMMENT '资源url',
`sort` int(32) DEFAULT NULL COMMENT '排序',
PRIMARY KEY (`spid`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of sys_permissions
-- ----------------------------
INSERT INTO `sys_permissions` VALUES ('1', '用户管理', '后台菜单', 'manager:user', '0', '1', null, '2');
INSERT INTO `sys_permissions` VALUES ('2', '角色管理', '后台菜单', 'manager:role', '0', '1', null, '3');
INSERT INTO `sys_permissions` VALUES ('3', '权限管理', '后台菜单', 'manager:permission', '0', '1', null, '4');
INSERT INTO `sys_permissions` VALUES ('4', '添加用户', '后台菜单', 'user:add', '1', '1', '/manager/user/save', '1');
INSERT INTO `sys_permissions` VALUES ('5', '获取用户', '后台菜单', 'user:query', '1', '1', '/manager/user/query', '2');
INSERT INTO `sys_permissions` VALUES ('6', '添加角色', '后台菜单', 'role:add', '2', '1', '/manager/role/save', '1');
INSERT INTO `sys_permissions` VALUES ('7', '获取角色', '后台菜单', 'role:query', '2', '1', '/manager/role/query', '2');
INSERT INTO `sys_permissions` VALUES ('8', '添加权限', '后台菜单', 'permission:add', '3', '1', '/manager/permission/save', '2');
INSERT INTO `sys_permissions` VALUES ('9', '获取权限', '后台菜单', 'permission:query', '3', '1', '/manager/permission/query', '1');
INSERT INTO `sys_permissions` VALUES ('10', '删除用户', '后台按钮', 'admin:deluser', '-1', '0', '/api/manager/user/*', '-1');
INSERT INTO `sys_permissions` VALUES ('12', '删除权限', '后台按钮', 'admin:delrole', '-1', '0', '/api/manager/permission/*', '-1');
INSERT INTO `sys_permissions` VALUES ('13', '前台查看', '后台菜单', 'user', '0', '1', null, '-1');
INSERT INTO `sys_permissions` VALUES ('14', '首页', '前台菜单', 'user:index', '13', '1', '/index', '1');
INSERT INTO `sys_permissions` VALUES ('15', '博客', '前台菜单', 'user:blog', '13', '1', '/blog', '2');
INSERT INTO `sys_permissions` VALUES ('16', '工作', '前台菜单', 'user:portfolio', '13', '1', '/portfolio', '3');
INSERT INTO `sys_permissions` VALUES ('17', '关于我们', '前台菜单', 'user:about', '13', '1', '/about', '4');
INSERT INTO `sys_permissions` VALUES ('18', '我们的记录', '前台菜单', 'user:love', '13', '1', '/love', '5');
INSERT INTO `sys_permissions` VALUES ('19', '我的联系方式', '前台菜单', 'user:contact', '0', '1', '/contact', '-1');
-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`srid` int(128) NOT NULL AUTO_INCREMENT,
`role` varchar(128) DEFAULT NULL,
`description` varchar(128) DEFAULT NULL,
`available` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`srid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES ('1', 'admin', '超级管理员', '0');
INSERT INTO `sys_role` VALUES ('2', 'user', '普通用户', '0');
-- ----------------------------
-- Table structure for sys_role_permissions
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_permissions`;
CREATE TABLE `sys_role_permissions` (
`spid` int(128) DEFAULT NULL,
`srid` int(128) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of sys_role_permissions
-- ----------------------------
INSERT INTO `sys_role_permissions` VALUES ('14', '2');
INSERT INTO `sys_role_permissions` VALUES ('15', '2');
INSERT INTO `sys_role_permissions` VALUES ('16', '2');
INSERT INTO `sys_role_permissions` VALUES ('17', '2');
INSERT INTO `sys_role_permissions` VALUES ('18', '2');
INSERT INTO `sys_role_permissions` VALUES ('19', '2');
INSERT INTO `sys_role_permissions` VALUES ('1', '1');
INSERT INTO `sys_role_permissions` VALUES ('2', '1');
INSERT INTO `sys_role_permissions` VALUES ('3', '1');
INSERT INTO `sys_role_permissions` VALUES ('4', '1');
INSERT INTO `sys_role_permissions` VALUES ('5', '1');
INSERT INTO `sys_role_permissions` VALUES ('6', '1');
INSERT INTO `sys_role_permissions` VALUES ('7', '1');
INSERT INTO `sys_role_permissions` VALUES ('8', '1');
INSERT INTO `sys_role_permissions` VALUES ('9', '1');
INSERT INTO `sys_role_permissions` VALUES ('10', '1');
INSERT INTO `sys_role_permissions` VALUES ('12', '1');
INSERT INTO `sys_role_permissions` VALUES ('14', '1');
-- ----------------------------
-- Table structure for sys_role_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_user`;
CREATE TABLE `sys_role_user` (
`suid` varchar(128) DEFAULT NULL,
`srid` int(128) DEFAULT NULL,
KEY `suid` (`suid`) USING BTREE,
CONSTRAINT `sys_role_user_ibfk_1` FOREIGN KEY (`suid`) REFERENCES `sys_user` (`suid`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of sys_role_user
-- ----------------------------
INSERT INTO `sys_role_user` VALUES ('0qm1j7rtoegtbo9qklvt45dab4', '2');
INSERT INTO `sys_role_user` VALUES ('0qm1j7rtoegtbo9qklvt45dac3', '2');
INSERT INTO `sys_role_user` VALUES ('t4f10snovmi14r5bot7u9ckm90', '1');
INSERT INTO `sys_role_user` VALUES ('s7d8mlboh8is5ovnd519qa8ehv', '2');
INSERT INTO `sys_role_user` VALUES ('t4f10snovio14r5bot7u9ckm72', '2');
INSERT INTO `sys_role_user` VALUES ('4fjkppf1i2is7rn2a6bbk8tgck', '2');
INSERT INTO `sys_role_user` VALUES ('7832ap5ffmievo880v8s13gqhe', '2');
-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`suid` varchar(128) NOT NULL,
`username` varchar(28) DEFAULT NULL,
`password` varchar(18) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL COMMENT '???',
`email` varchar(128) DEFAULT NULL COMMENT '??',
`QQ` varchar(16) DEFAULT NULL,
`state` varchar(128) DEFAULT NULL COMMENT '?????0:????1:??,2:?????',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`suid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES ('0qm1j7rtoegtbo9qklvt45dab4', 'Somer', 'qwer150407!@?', '13333333333', 'xuyangsomer@outlook.com', '154156123', '1', '2017-11-20 10:24:41', '2017-11-20 10:24:41');
INSERT INTO `sys_user` VALUES ('0qm1j7rtoegtbo9qklvt45dac3', 'Xwl', '258000', '13333333333', '154156123@qq.com', '154156123', '1', '2017-11-19 11:28:16', '2017-11-19 11:28:16');
INSERT INTO `sys_user` VALUES ('4fjkppf1i2is7rn2a6bbk8tgck', 'ceshi', '123456', '13333333333', '154156123@qq.com', '154156123', '1', '2017-12-12 21:25:53', '2017-12-12 21:25:53');
INSERT INTO `sys_user` VALUES ('7832ap5ffmievo880v8s13gqhe', 'ceshi2', '111111', '13333333333', '154156123@qq.com', '154156123', '1', '2017-12-28 16:58:56', '2017-12-28 16:58:56');
INSERT INTO `sys_user` VALUES ('s7d8mlboh8is5ovnd519qa8ehv', 'zhanglei', '123456', '13333333333', '154156123@qq.com', '154156123', '1', '2017-11-19 11:35:13', '2017-11-19 11:35:13');
INSERT INTO `sys_user` VALUES ('t4f10snovio14r5bot7u9ckm72', 'linv2', '123456', '13333333333', '154156123@qq.com', '154156123', '1', '2017-12-05 23:10:31', '2017-12-07 21:07:43');
INSERT INTO `sys_user` VALUES ('t4f10snovmi14r5bot7u9ckm90', 'admin', '123456', '13333333333', '154156123@qq.com', '154156123', '1', '2017-11-19 11:13:52', '2017-11-19 11:13:52');
maven 依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.somuns</groupId>
<artifactId>somuns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>somuns</name>
<description>somuns</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<druid.version>1.1.4</druid.version>
<nutz.version>1.r.63-SNAPSHOT</nutz.version>
<beetl.version>2.7.23</beetl.version>
<shiro.version>1.4.0</shiro.version>
<activiti.version>6.0.0</activiti.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>${beetl.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz-plugins-spring-boot-starter</artifactId>
<version>${nutz.version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz-integration-spring</artifactId>
<version>${nutz.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2.1</version>
</dependency>
<!--开启aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-process-validation</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>nutzcn-snapshots</id>
<url>https://jfrog.nutz.cn/artifactory/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>nexus</id>
<name> Nexus release repository</name>
<url>http://nexus.gzzgtech.net/repository/maven-public/</url>
</repository>
</repositories>
<build>
<finalName>somuns</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-resources-plugin</artifactId>-->
<!--<configuration>-->
<!--<nonFilteredFileExtensions>-->
<!--<nonFilteredFileExtension>p12</nonFilteredFileExtension>-->
<!--<nonFilteredFileExtension>Jsk</nonFilteredFileExtension>-->
<!--</nonFilteredFileExtensions>-->
<!--</configuration>-->
<!--</plugin>-->
</plugins>
</build>
</project>
程序入口
package net.somuns;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@SpringBootApplication
public class SomunsApplication {
public static void main(String[] args) {
SpringApplication.run(SomunsApplication.class, args);
}
}
数据连接池配置:
package net.somuns.config;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Somuns on 2017/11/8.
*/
@Configuration
public class DruidConfigure {
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean druid = new ServletRegistrationBean();
druid.setServlet(new StatViewServlet());
druid.setUrlMappings(Arrays.asList("/druid/*"));
Map<String, String> params = new HashMap<String, String>();
params.put("loginUsername", "admin");
params.put("loginPassword", "admin");
druid.setInitParameters(params);
return druid;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean fitler = new FilterRegistrationBean();
fitler.setFilter(new WebStatFilter());
fitler.setUrlPatterns(Arrays.asList("/*"));
fitler.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return fitler;
}
}
beetl模板配置:
package net.somuns.config;
import net.somuns.config.bean.ResourceBean;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.FileResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Somuns on 2017/11/8.VelocityAutoConfiguration
*/
@Configuration
public class BeetlConfigure {
@Autowired
ResourceBean resourceBean;
@Bean(initMethod = "init", name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver = ResourcePatternUtils
.getResourcePatternResolver(new DefaultResourceLoader());
try {
FileResourceLoader fileResourceLoader = new FileResourceLoader(resourceBean.getHtmlPath());
beetlGroupUtilConfiguration.setResourceLoader(fileResourceLoader);
beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.yml"));
Map<String, Object> runtimeVars = new HashMap<>();
//runtimeVars.put("respath", "");//根据自己js等静态文件路径配置,因为没引用,所以没配置
beetlGroupUtilConfiguration.setSharedVars(runtimeVars);
return beetlGroupUtilConfiguration;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResolver(
@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setSuffix(".html");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
return beetlSpringViewResolver;
}
}
ShiroConfig配置:
package net.somuns.config;
import net.somuns.model.system.SysPermissions;
import net.somuns.model.system.SysRole;
import net.somuns.service.permissions.IServicePermissions;
import net.somuns.utils.RuntimeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Somuns on 2017/11/9.
*/
@Configuration
public class ShiroConfigure {
@Autowired
private IServicePermissions permissions;
/**
* ShiroFilterFactoryBean 处理拦截资源文件问题。
* 注意:单独一个ShiroFilterFactoryBean配置是或报错的,以为在
* 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager
* <p>
* Filter Chain定义说明
* 1、一个URL可以配置多个Filter,使用逗号分隔
* 2、当设置多个过滤器时,全部验证通过,才视为通过
* 3、部分过滤器可指定参数,如perms,roles
*/
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
System.out.println("》》》》》》》》Shiro过滤器");
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
// 必须设置 SecurityManager
shiroFilterFactoryBean.setSecurityManager(securityManager);
//拦截器.getPermissions
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
//配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了.
filterChainDefinitionMap.put("/logout", "logout");
filterChainDefinitionMap.put("/authentication", "anon");
filterChainDefinitionMap.put("/vcode", "anon");
filterChainDefinitionMap.put("/api/user/authentication", "anon");
filterChainDefinitionMap.put("/register", "anon");
filterChainDefinitionMap.put("/api/user/register", "anon");
filterChainDefinitionMap.put("/api/user/login", "anon");
filterChainDefinitionMap.put("/admin/login", "anon");
filterChainDefinitionMap.put("/api/admin/login", "anon");
filterChainDefinitionMap.put("/resources/**", "anon");
List<SysPermissions> pList = permissions.getAllPermissions();
for (SysPermissions p :pList){
if (null!=p.getUrl()&&p.getPermission()!=null){
filterChainDefinitionMap.put(p.getUrl(),"perms["+p.getPermission()+"]");
}
}
filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setLoginUrl("/login");
// 登录成功后要跳转的链接
shiroFilterFactoryBean.setSuccessUrl("/index");
//未授权界面;
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
return securityManager;
}
@Bean
public MyShiroRealm myShiroRealm() {
MyShiroRealm realm = new MyShiroRealm();
return realm;
}
}
实现认证和授权部分
ShiroRealm:
package net.somuns.config;
import net.somuns.model.system.SysPermissions;
import net.somuns.model.system.SysRole;
import net.somuns.service.permissions.IServicePermissions;
import net.somuns.utils.RuntimeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Somuns on 2017/11/9.
*/
@Configuration
public class ShiroConfigure {
@Autowired
private IServicePermissions permissions;
/**
* ShiroFilterFactoryBean 处理拦截资源文件问题。
* 注意:单独一个ShiroFilterFactoryBean配置是或报错的,以为在
* 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager
* <p>
* Filter Chain定义说明
* 1、一个URL可以配置多个Filter,使用逗号分隔
* 2、当设置多个过滤器时,全部验证通过,才视为通过
* 3、部分过滤器可指定参数,如perms,roles
*/
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
System.out.println("》》》》》》》》Shiro过滤器");
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
// 必须设置 SecurityManager
shiroFilterFactoryBean.setSecurityManager(securityManager);
//拦截器.getPermissions
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
//配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了.
filterChainDefinitionMap.put("/logout", "logout");
filterChainDefinitionMap.put("/authentication", "anon");
filterChainDefinitionMap.put("/vcode", "anon");
filterChainDefinitionMap.put("/api/user/authentication", "anon");
filterChainDefinitionMap.put("/register", "anon");
filterChainDefinitionMap.put("/api/user/register", "anon");
filterChainDefinitionMap.put("/api/user/login", "anon");
filterChainDefinitionMap.put("/admin/login", "anon");
filterChainDefinitionMap.put("/api/admin/login", "anon");
filterChainDefinitionMap.put("/resources/**", "anon");
List<SysPermissions> pList = permissions.getAllPermissions();
for (SysPermissions p :pList){
if (null!=p.getUrl()&&p.getPermission()!=null){
filterChainDefinitionMap.put(p.getUrl(),"perms["+p.getPermission()+"]");
}
}
filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setLoginUrl("/login");
// 登录成功后要跳转的链接
shiroFilterFactoryBean.setSuccessUrl("/index");
//未授权界面;
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
return securityManager;
}
@Bean
public MyShiroRealm myShiroRealm() {
MyShiroRealm realm = new MyShiroRealm();
return realm;
}
}
项目实例项目后台管理效果图:
用户注册界面:
管理员登录界面
用户管理界面:
角色管理界面
权限管理界面
普通用户界面:
普通用户界面,功能没有写。只贴了个简单前端模板
server:
port: 80
# ssl:
# key-store: classpath:keystore.p12
# key-store-password: 154156570
# key-store-type: PKCS12
# key-alias: tomcat
logging:
level:
root: info
org.nutz: debug
somuns: debug
spring:
datasource:
url: jdbc:mysql://localhost:3306/somuns?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
aop:
auto: true
proxy-target-class: true
devtools:
restart:
additional-paths: src/main/
mail:
host: smtp.qq.com
username: xxx
password: xxx
default-encoding: UTF-8
port: 587
auth: true
protocol: smtp
authkey: mail.smtp.auth
timeout: 25000
timeoutkey: mail.smtp.timeout
loglevel: true
loglevelkey: mail.smtp.debug
path:
#前端代码地址
html: D:\workspace\somuns\somuns-web\
nutz:
#配置后js才可以解析object转换的json
json:
auto-unicode: false
quote-name: true
ignore-null: true
null-as-emtry: true
dao:
runtime:
create: true
basepackage: net.somuns.model.system
项目实例地址: