argumentResolvers) {
20 | argumentResolvers.add(new SessionUserArgumentResolver());
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/spring-boot-shiro-autoconfigure/src/main/java/com/millinch/spring/boot/autoconfigure/shiro/annotation/EnableShiroWebSupport.java:
--------------------------------------------------------------------------------
1 | package com.millinch.spring.boot.autoconfigure.shiro.annotation;
2 |
3 | import com.millinch.spring.boot.autoconfigure.shiro.ShiroWebMvcConfigurerAdapter;
4 | import org.springframework.context.annotation.Import;
5 | import org.springframework.context.annotation.ImportSelector;
6 | import org.springframework.core.type.AnnotationMetadata;
7 |
8 | import java.lang.annotation.*;
9 |
10 | /**
11 | * Annotation to automatically register the following beans for usage with Spring MVC.
12 | *
13 | * -
14 | * {@link com.millinch.spring.boot.autoconfigure.shiro.annotation.SessionUser}.
15 | *
16 | *
17 | * @author John Zhang
18 | */
19 | @Retention(RetentionPolicy.RUNTIME)
20 | @Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
21 | @Inherited
22 | @Import({ EnableShiroWebSupport.ShiroWebMvcConfigurerAdapterImportSelector.class })
23 | public @interface EnableShiroWebSupport {
24 |
25 | static class ShiroWebMvcConfigurerAdapterImportSelector implements ImportSelector {
26 |
27 | @Override
28 | public String[] selectImports(AnnotationMetadata importingClassMetadata) {
29 | return new String[] { ShiroWebMvcConfigurerAdapter.class.getName() };
30 | }
31 |
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/spring-boot-shiro-autoconfigure/src/main/java/com/millinch/spring/boot/autoconfigure/shiro/annotation/SessionUser.java:
--------------------------------------------------------------------------------
1 | package com.millinch.spring.boot.autoconfigure.shiro.annotation;
2 |
3 | import java.lang.annotation.*;
4 |
5 | /**
6 | * 获取Shiro当前用户
7 | * @author 张劲航
8 | * @see SessionUserArgumentResolver
9 | */
10 | @Target({ElementType.PARAMETER})
11 | @Retention(RetentionPolicy.RUNTIME)
12 | @Documented
13 | public @interface SessionUser {
14 | }
15 |
--------------------------------------------------------------------------------
/spring-boot-shiro-autoconfigure/src/main/java/com/millinch/spring/boot/autoconfigure/shiro/annotation/SessionUserArgumentResolver.java:
--------------------------------------------------------------------------------
1 | package com.millinch.spring.boot.autoconfigure.shiro.annotation;
2 |
3 | import org.apache.shiro.SecurityUtils;
4 | import org.apache.shiro.subject.Subject;
5 | import org.springframework.core.MethodParameter;
6 | import org.springframework.web.bind.support.WebDataBinderFactory;
7 | import org.springframework.web.context.request.NativeWebRequest;
8 | import org.springframework.web.method.support.HandlerMethodArgumentResolver;
9 | import org.springframework.web.method.support.ModelAndViewContainer;
10 |
11 | /**
12 | * {@link SessionUser}注解的解析
13 | * @author 张劲航
14 | */
15 | public class SessionUserArgumentResolver implements HandlerMethodArgumentResolver {
16 |
17 | @Override
18 | public boolean supportsParameter(MethodParameter parameter) {
19 | return parameter.hasParameterAnnotation(SessionUser.class);
20 | }
21 |
22 | @Override
23 | public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
24 | Subject subject = SecurityUtils.getSubject();
25 | if(supportsParameter(parameter) && subject.isAuthenticated()){
26 | return subject.getPrincipal();
27 | }
28 | return null;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/spring-boot-shiro-autoconfigure/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | # Auto Configure
2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
3 | com.millinch.spring.boot.autoconfigure.shiro.ShiroAutoConfiguration
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.springframework.boot
8 | spring-boot-starter-parent
9 | 1.3.5.RELEASE
10 |
11 | com.millinch
12 | spring-boot-shiro-sample
13 | 1.0-SNAPSHOT
14 |
15 |
16 |
17 |
18 | org.springframework.boot
19 | spring-boot-starter-web
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-data-jpa
24 |
25 |
26 | com.millinch
27 | spring-boot-shiro-starter
28 | 1.0.0
29 |
30 |
31 | mysql
32 | mysql-connector-java
33 | 5.1.38
34 |
35 |
36 | joda-time
37 | joda-time
38 | 2.9.4
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-maven-plugin
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/java/com/millinch/springboot/shiro/sample/SampleApplication.java:
--------------------------------------------------------------------------------
1 | package com.millinch.springboot.shiro.sample;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * This guy is lazy, nothing left.
8 | *
9 | * @author John Zhang
10 | */
11 | @SpringBootApplication
12 | public class SampleApplication {
13 |
14 | public static void main(String[] args) {
15 | SpringApplication.run(SampleApplication.class, args);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/java/com/millinch/springboot/shiro/sample/domain/User.java:
--------------------------------------------------------------------------------
1 | package com.millinch.springboot.shiro.sample.domain;
2 |
3 | /**
4 | * This guy is lazy, nothing left.
5 | * @author John Zhang
6 | */
7 | public class User {
8 |
9 | private String username;
10 | private String password;
11 |
12 | public String getUsername() {
13 | return username;
14 | }
15 |
16 | public void setUsername(String username) {
17 | this.username = username;
18 | }
19 |
20 | public String getPassword() {
21 | return password;
22 | }
23 |
24 | public void setPassword(String password) {
25 | this.password = password;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/java/com/millinch/springboot/shiro/sample/realm/FirstRealm.java:
--------------------------------------------------------------------------------
1 | package com.millinch.springboot.shiro.sample.realm;
2 |
3 | import org.apache.shiro.realm.jdbc.JdbcRealm;
4 |
5 | /**
6 | * This guy is lazy, nothing left.
7 | *
8 | * @author John Zhang
9 | */
10 | public class FirstRealm extends JdbcRealm {
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/java/com/millinch/springboot/shiro/sample/web/MyFilter.java:
--------------------------------------------------------------------------------
1 | package com.millinch.springboot.shiro.sample.web;
2 |
3 | import javax.servlet.*;
4 | import java.io.IOException;
5 |
6 | /**
7 | * This guy is lazy, nothing left.
8 | *
9 | * @author John Zhang
10 | */
11 | public class MyFilter implements Filter {
12 | @Override
13 | public void init(FilterConfig filterConfig) throws ServletException {
14 |
15 | }
16 |
17 | @Override
18 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
19 | System.out.println("My Filter executed!!");
20 | filterChain.doFilter(servletRequest, servletResponse);
21 | }
22 |
23 | @Override
24 | public void destroy() {
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/java/com/millinch/springboot/shiro/sample/web/SampleApi.java:
--------------------------------------------------------------------------------
1 | package com.millinch.springboot.shiro.sample.web;
2 |
3 | import org.apache.shiro.SecurityUtils;
4 | import org.apache.shiro.subject.Subject;
5 | import org.springframework.http.ResponseEntity;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 |
9 | /**
10 | * This guy is lazy, nothing left.
11 | *
12 | * @author John Zhang
13 | */
14 | @Controller
15 | public class SampleApi {
16 |
17 | @RequestMapping("/role/config")
18 | public ResponseEntity roleConfig() {
19 | return ResponseEntity.ok("Here you are");
20 | }
21 |
22 | @RequestMapping("/user/addition")
23 | public ResponseEntity userAddition() {
24 | Subject subject = SecurityUtils.getSubject();
25 | boolean permitted = subject.isPermitted("system:admin:create");
26 | System.out.println("permitted = " + permitted);
27 | return ResponseEntity.ok("Here you are");
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 7788
3 | spring:
4 | datasource:
5 | url: jdbc:mysql://localhost:3306/test
6 | username: root
7 | password: root
8 | driver-class-name: com.mysql.jdbc.Driver
9 | platform: mysql
10 |
11 | shiro:
12 | realm:
13 | jdbc:
14 | enabled: true
15 | authentication-query: SELECT password FROM sys_user where user_name = ? #根据用户名获取密码
16 | salt: no_salt #可选值:no_salt, crypt(源码中未实现), column(上面这个SQL中第二列中获取salt), external(需继承JdbcRealm重写getSaltForUser()方法)
17 | user-roles-query: SELECT r.code
18 | FROM sys_user_role ur
19 | LEFT JOIN sys_role r ON r.id = ur.role_id
20 | LEFT JOIN sys_user u ON ur.user_id = u.id
21 | WHERE u.user_name = ? #根据用户名获取角色
22 | permissions-query: SELECT re.permission
23 | FROM sys_role_resource rr
24 | LEFT JOIn sys_resource re ON rr.resource_id = re.id
25 | LEFT JOIN sys_role r ON rr.role_id = r.id
26 | WHERE r.code = ? #根据角色获取权限
27 | permissions-lookup-enabled: true
28 | login-url: /login.html #登录入口URL
29 | success-url: /index.html #登录成功跳转URL
30 | unauthorized-url: /unauthorized.html #当访问未授权页面时跳转至该URL,将为filter chain中的每个AuthorizationFilter设置跳转URL(如果目标没有指定)
31 | sign-in:
32 | user-param: username #用户名参数名称
33 | password-param: password #密码参数名称
34 | remember-me-param: rememberMe #记住我参数名称
35 | hash-iterations: 1 #加密迭代次数,强制设为至少1次(即使设置0或负数)
36 | hash-algorithm-name: MD5 #加密算法名称,如:MD2/SHA-1/SHA-256/SHA-384/SHA-512
37 | filters:
38 | my-filter: com.millinch.springboot.shiro.sample.web.MyFilter
39 | filter-chain-definitions: #默认为空,一般如下配置
40 | /test-my-filter: my-filter
41 | /logout: logout
42 | /favicon.ico: anon
43 | /assets/**: anon
44 | /**: authc
45 | filter-chain-sql: SELECT r.url, r.permission
46 | FROM sys_resource r
47 | WHERE r.permission != '' AND r.url != ''
48 | logging:
49 | level: debug
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/resources/data-mysql.sql:
--------------------------------------------------------------------------------
1 | -- ----------------------------
2 | -- Records of sys_user
3 | -- ----------------------------
4 | INSERT INTO `sys_user` VALUES ('1', '18966668888', 'super', '超级管理员', 'e10adc3949ba59abbe56e057f20f883e', null, null, null, null, '', null, null, null, null, null, '1', null, '2015-09-28 17:47:18', null, '2015-09-30 17:36:16');
5 | INSERT INTO `sys_user` VALUES ('2', '13988886666', 'admin', '系统管理员A', 'e10adc3949ba59abbe56e057f20f883e', null, null, null, '1234567', 'super@millinch.com', null, null, null, null, null, '1', null, '2015-09-29 17:47:22', null, '2015-09-30 17:32:07');
6 |
7 | -- ----------------------------
8 | -- Records of sys_role
9 | -- ----------------------------
10 | INSERT INTO `sys_role` VALUES ('1', '超级管理员', 'superManager', '拥有所有权限', '1', null, '0', '2015-09-01 14:36:16', null, '2016-01-03 22:29:58');
11 | INSERT INTO `sys_role` VALUES ('2', '系统管理员', 'systemManager', '拥有部分权限', '1', null, '0', '2015-08-30 18:03:47', null, '2015-08-30 18:03:47');
12 | INSERT INTO `sys_role` VALUES ('3', '角色1', 'role1', 'nothing 34', '1', null, null, '2015-10-05 18:20:35', null, '2015-10-05 18:35:57');
13 |
14 | -- ----------------------------
15 | -- Records of sys_resource
16 | -- ----------------------------
17 | INSERT INTO `sys_resource` VALUES ('1', '菜单', '系统管理', 'system:*', 'fa fa-dashboard', '1', '', '', '1', '0', '0', '2015-07-01 19:33:21', null, '2015-10-09 10:34:05');
18 | INSERT INTO `sys_resource` VALUES ('2', '菜单', '角色管理', 'system:role:*', 'fa fa-male', '12', '/role/config', '', '1', '1', '0', '2015-07-01 19:38:38', null, '2015-07-01 19:38:38');
19 | INSERT INTO `sys_resource` VALUES ('3', '菜单', '密码修改', 'system:password', null, '14', '/user/password/edition', '', '1', '1', '0', '2015-07-01 19:38:51', null, '2015-07-01 19:39:51');
20 | INSERT INTO `sys_resource` VALUES ('4', '菜单', '操作日志', 'system:log:*', null, '15', '/handle/operation/log', '', '1', '1', '0', '2015-07-01 19:40:37', null, '2015-07-01 19:40:37');
21 | INSERT INTO `sys_resource` VALUES ('5', 'URL', '新增角色', 'system:role:create', null, '13', '/role/addition', '', '1', '1', '0', '2015-07-01 19:41:21', null, '2015-10-08 16:45:43');
22 | INSERT INTO `sys_resource` VALUES ('6', '菜单', '用户管理', 'system:admin:*', 'fa fa-users', '11', '/user/config', '', '1', '1', '0', '2015-07-01 19:34:38', null, '2015-07-01 19:34:38');
23 | INSERT INTO `sys_resource` VALUES ('7', 'URL', '新增用户', 'system:admin:create', '', null, '/user/addition', 'bbb', '1', '0', '0', '2015-08-30 18:29:56', null, '2015-10-09 11:33:03');
24 |
25 | -- ----------------------------
26 | -- Records of sys_role_resource
27 | -- ----------------------------
28 | INSERT INTO `sys_role_resource` VALUES ('1', '1', '1');
29 | INSERT INTO `sys_role_resource` VALUES ('2', '1', '2');
30 | INSERT INTO `sys_role_resource` VALUES ('3', '1', '3');
31 | INSERT INTO `sys_role_resource` VALUES ('4', '1', '4');
32 | INSERT INTO `sys_role_resource` VALUES ('5', '1', '5');
33 | INSERT INTO `sys_role_resource` VALUES ('6', '1', '6');
34 | INSERT INTO `sys_role_resource` VALUES ('7', '1', '7');
35 | INSERT INTO `sys_role_resource` VALUES ('8', '2', '2');
36 |
37 | -- ----------------------------
38 | -- Records of sys_user_role
39 | -- ----------------------------
40 | INSERT INTO `sys_user_role` VALUES ('1', '1', '1');
41 | INSERT INTO `sys_user_role` VALUES ('2', '2', '2');
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/resources/schema-mysql.sql:
--------------------------------------------------------------------------------
1 | -- ----------------------------
2 | -- Table structure for sys_resource
3 | -- ----------------------------
4 | DROP TABLE IF EXISTS `sys_resource`;
5 | CREATE TABLE `sys_resource` (
6 | `id` bigint(20) NOT NULL AUTO_INCREMENT,
7 | `type` varchar(255) DEFAULT '' COMMENT '权限类型:menu、button、url',
8 | `name` varchar(255) NOT NULL COMMENT '权限名称',
9 | `permission` varchar(255) NOT NULL COMMENT '权限字符串',
10 | `icon` varchar(255) DEFAULT NULL,
11 | `sort` int(11) DEFAULT '0',
12 | `url` varchar(255) DEFAULT '',
13 | `description` varchar(255) DEFAULT '' COMMENT '资源描述',
14 | `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态值',
15 | `parent_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '父ID',
16 | `create_by` bigint(20) DEFAULT NULL,
17 | `create_at` datetime NOT NULL,
18 | `update_by` bigint(20) DEFAULT NULL,
19 | `update_at` datetime DEFAULT NULL,
20 | PRIMARY KEY (`id`),
21 | UNIQUE KEY `uq_resource_type` (`type`,`permission`) USING BTREE,
22 | KEY `create_by` (`create_by`),
23 | KEY `update_by` (`update_by`)
24 | ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='资源(权限)表';
25 |
26 |
27 | -- ----------------------------
28 | -- Table structure for sys_role
29 | -- ----------------------------
30 | DROP TABLE IF EXISTS `sys_role`;
31 | CREATE TABLE `sys_role` (
32 | `id` bigint(20) NOT NULL AUTO_INCREMENT,
33 | `name` varchar(255) NOT NULL,
34 | `code` varchar(255) NOT NULL,
35 | `remark` varchar(255) DEFAULT '',
36 | `status` tinyint(1) NOT NULL DEFAULT '1',
37 | `parent_id` bigint(20) DEFAULT NULL,
38 | `create_by` bigint(20) DEFAULT NULL,
39 | `create_at` datetime NOT NULL,
40 | `update_by` bigint(20) DEFAULT NULL,
41 | `update_at` datetime DEFAULT NULL,
42 | PRIMARY KEY (`id`),
43 | KEY `create_by` (`create_by`),
44 | KEY `update_by` (`update_by`)
45 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
46 |
47 |
48 | -- ----------------------------
49 | -- Table structure for sys_role_resource
50 | -- ----------------------------
51 | DROP TABLE IF EXISTS `sys_role_resource`;
52 | CREATE TABLE `sys_role_resource` (
53 | `id` bigint(20) NOT NULL AUTO_INCREMENT,
54 | `role_id` bigint(20) DEFAULT NULL,
55 | `resource_id` bigint(20) DEFAULT NULL,
56 | PRIMARY KEY (`id`),
57 | KEY `role_id` (`role_id`),
58 | KEY `resource_id` (`resource_id`)
59 | ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
60 |
61 | -- ----------------------------
62 | -- Table structure for sys_user
63 | -- ----------------------------
64 | DROP TABLE IF EXISTS `sys_user`;
65 | CREATE TABLE `sys_user` (
66 | `id` bigint(20) NOT NULL AUTO_INCREMENT,
67 | `mobile_phone` varchar(255) NOT NULL DEFAULT '' COMMENT '手机号码',
68 | `user_name` varchar(50) DEFAULT NULL COMMENT '用户名',
69 | `nickname` varchar(255) DEFAULT NULL COMMENT '昵称',
70 | `password` varchar(255) NOT NULL COMMENT '密码',
71 | `salt` varchar(255) DEFAULT '' COMMENT '加密混淆字符',
72 | `signature` varchar(255) DEFAULT NULL COMMENT '个性签名',
73 | `gender` tinyint(1) DEFAULT '0' COMMENT '性别',
74 | `qq` bigint(20) DEFAULT NULL COMMENT 'QQ号码',
75 | `email` varchar(255) DEFAULT NULL COMMENT '邮箱地址',
76 | `avatar` varchar(500) DEFAULT '' COMMENT '头像图片路径',
77 | `province` varchar(50) DEFAULT '' COMMENT '省',
78 | `city` varchar(50) DEFAULT '' COMMENT '市',
79 | `reg_ip` varchar(50) DEFAULT NULL COMMENT '注册时IP地址',
80 | `score` int(10) DEFAULT '0' COMMENT '积分值',
81 | `status` int(10) DEFAULT '1' COMMENT '状态:0禁用 1正常',
82 | `create_by` bigint(20) DEFAULT NULL,
83 | `create_at` datetime DEFAULT NULL,
84 | `update_by` bigint(20) DEFAULT NULL,
85 | `update_at` datetime DEFAULT NULL,
86 | PRIMARY KEY (`id`),
87 | UNIQUE KEY `uq_user_name` (`user_name`)
88 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='用户表';
89 |
90 | -- ----------------------------
91 | -- Table structure for sys_user_role
92 | -- ----------------------------
93 | DROP TABLE IF EXISTS `sys_user_role`;
94 | CREATE TABLE `sys_user_role` (
95 | `id` bigint(20) NOT NULL AUTO_INCREMENT,
96 | `user_id` bigint(20) NOT NULL,
97 | `role_id` bigint(20) NOT NULL,
98 | PRIMARY KEY (`id`),
99 | KEY `fk_user_roles` (`user_id`),
100 | KEY `fk_role_users` (`role_id`)
101 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
102 |
103 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/resources/static/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Demo - Apache Shiro integration with Spring Boot
6 |
7 |
8 |
9 |
10 |
11 |
Welcome!
12 |
退出
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/resources/static/login.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 登录
6 |
7 |
8 |
9 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/spring-boot-shiro-sample/src/main/resources/static/unauthorized.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Unauthorized
6 |
7 |
8 | 没有权限
9 |
10 |
--------------------------------------------------------------------------------
/spring-boot-shiro-starter/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.millinch
8 | spring-boot-shiro-starter
9 | 1.0.0
10 |
11 |
12 | 1.3.3.RELEASE
13 |
14 |
15 |
16 |
17 |
18 | org.springframework.boot
19 | spring-boot-dependencies
20 | ${spring-boot.version}
21 | pom
22 | import
23 |
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter
30 |
31 |
32 | com.millinch
33 | spring-boot-shiro-autoconfigure
34 | 1.0.0
35 |
36 |
37 |
--------------------------------------------------------------------------------
/spring-boot-shiro-starter/src/main/resources/META-INF/spring.provides:
--------------------------------------------------------------------------------
1 | provides: spring-boot-shiro-autoconfigure
--------------------------------------------------------------------------------