Skip to content

Commit 2f35828

Browse files
committed
ssm+shiro demo
1 parent d4da2f4 commit 2f35828

28 files changed

Lines changed: 1228 additions & 1 deletion

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
# Package Files
88
*.jar
99
*.war
10-
*.zip
10+
*.zip
11+
12+
.settings
13+
**/target
14+
.classpath
15+
.project

cg/ReadMe.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 说明文档
2+
## 环境介绍
3+
Spring+SpringMVC+Mybatis+MySQL+Shiro+Maven
4+
## 运行过程
5+
### 1.用eclipse导入
6+
应该可以看到如下目录结构:
7+
8+
![](http://i.imgur.com/f76eI6u.png)
9+
10+
### 2.执行SQL脚本
11+
SQL脚本位于webapp->WEB-INF->sql下,该脚本在MySQL5.7上测试通过。
12+
主要有五张表:
13+
14+
![](http://i.imgur.com/HupJjhX.png)
15+
16+
由用户、角色和权限之间的关系不难得出这五张表。
17+
18+
### 3.修改项目配置
19+
要修改的主要是jdbc的连接配置,将jdbc.properties对应的属性修改成你本机的属性即可:
20+
21+
![](http://i.imgur.com/QoGWzSP.png)
22+
23+
### 4.确保Maven已导入相关依赖
24+
25+
---
26+
## 项目演示
27+
28+
为方便演示,sql脚本中已有相应数据:
29+
30+
有两个用户:admin和user
31+
32+
admin的role为admin,user的role为user
33+
34+
role["admin"]具有user:view,user:create,user:update,user:delete四种权限
35+
36+
role["user"]只拥有user:view的权限
37+
38+
39+
Shiro的核心配置如下:
40+
41+
![](http://i.imgur.com/ahXALOK.png)
42+
43+
接下来进行测试,启动项目,在浏览器中输入:
44+
45+
### http://localhost:8080/cg/index
46+
由于/index配置了authc过滤器,因此用户在未认证之前会跳到loginUrl,也就是[http://localhost:8080/cg/login](http://localhost:8080/cg/login)进行认证:
47+
48+
![](http://i.imgur.com/2FYS7za.png)
49+
50+
输入正确的用户名跟密码(user/123456)后登录成功,再输入[http://localhost:8080/cg/index](http://localhost:8080/cg/index),正常进入页面:
51+
52+
53+
![](http://i.imgur.com/wm6yDCC.png)
54+
55+
此时用户role为user,拥有的权限为user:view,输入:[http://localhost:8080/cg/index2](http://localhost:8080/cg/index2 "http://localhost:8080/cg/index2"),[http://localhost:8080/cg/user](http://localhost:8080/cg/user "http://localhost:8080/cg/user"),
56+
都可以正常访问,而[http://localhost:8080/cg/admin](http://localhost:8080/cg/admin "http://localhost:8080/cg/admin")则不能访问,由于用户已经登录,所以会跳到unauthorizedUrl即http://localhost:8080/cg/unauthorized,如图:
57+
58+
59+
![](http://i.imgur.com/xp6TkIx.png)
60+
61+
注销:[http://localhost:8080/cg/logout](http://localhost:8080/cg/logout "http://localhost:8080/cg/logout")
62+
之后会跳到登录页面,换成admin/123456登录再验证如上url。

cg/pom.xml

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>scut.legend</groupId>
5+
<artifactId>cg</artifactId>
6+
<packaging>war</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>cg Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<spring.version>4.1.6.RELEASE</spring.version>
14+
<shiro.version>1.2.2</shiro.version>
15+
</properties>
16+
17+
18+
<dependencies>
19+
<!-- junit依赖 -->
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.12</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<!-- 日志依赖slf4j+logback -->
27+
<dependency>
28+
<groupId>ch.qos.logback</groupId>
29+
<artifactId>logback-classic</artifactId>
30+
<version>1.1.7</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.logback-extensions</groupId>
34+
<artifactId>logback-ext-spring</artifactId>
35+
<version>0.1.1</version>
36+
</dependency>
37+
<!-- spring相关依赖 -->
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-context</artifactId>
41+
<version>${spring.version}</version>
42+
</dependency>
43+
<!-- springMVC -->
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-webmvc</artifactId>
47+
<version>${spring.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-jdbc</artifactId>
53+
<version>${spring.version}</version>
54+
</dependency>
55+
<!-- 数据库相关依赖 -->
56+
<!-- 数据库连接池 -->
57+
<dependency>
58+
<groupId>com.alibaba</groupId>
59+
<artifactId>druid</artifactId>
60+
<version>1.0.18</version>
61+
</dependency>
62+
<!-- mysql驱动 -->
63+
<dependency>
64+
<groupId>mysql</groupId>
65+
<artifactId>mysql-connector-java</artifactId>
66+
<version>5.1.38</version>
67+
</dependency>
68+
<!-- mybatis相关 -->
69+
<dependency>
70+
<groupId>org.mybatis</groupId>
71+
<artifactId>mybatis</artifactId>
72+
<version>3.4.1</version>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.mybatis</groupId>
77+
<artifactId>mybatis-spring</artifactId>
78+
<version>1.3.0</version>
79+
</dependency>
80+
81+
<!-- servlet API -->
82+
<dependency>
83+
<groupId>javax.servlet</groupId>
84+
<artifactId>javax.servlet-api</artifactId>
85+
<version>3.1.0</version>
86+
<scope>provided</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>jstl</groupId>
90+
<artifactId>jstl</artifactId>
91+
<version>1.2</version>
92+
<scope>provided</scope>
93+
</dependency>
94+
<!-- fastjson -->
95+
<dependency>
96+
<groupId>com.alibaba</groupId>
97+
<artifactId>fastjson</artifactId>
98+
<version>1.2.23</version>
99+
</dependency>
100+
101+
<!-- Shiro相关依赖 -->
102+
<dependency>
103+
<groupId>org.apache.shiro</groupId>
104+
<artifactId>shiro-core</artifactId>
105+
<version>${shiro.version}</version>
106+
</dependency>
107+
<dependency>
108+
<groupId>org.apache.shiro</groupId>
109+
<artifactId>shiro-web</artifactId>
110+
<version>${shiro.version}</version>
111+
</dependency>
112+
<dependency>
113+
<groupId>org.apache.shiro</groupId>
114+
<artifactId>shiro-spring</artifactId>
115+
<version>${shiro.version}</version>
116+
</dependency>
117+
<dependency>
118+
<groupId>org.apache.shiro</groupId>
119+
<artifactId>shiro-quartz</artifactId>
120+
<version>${shiro.version}</version>
121+
</dependency>
122+
<dependency>
123+
<groupId>org.apache.shiro</groupId>
124+
<artifactId>shiro-ehcache</artifactId>
125+
<version>${shiro.version}</version>
126+
</dependency>
127+
<dependency>
128+
<groupId>commons-fileupload</groupId>
129+
<artifactId>commons-fileupload</artifactId>
130+
<version>1.3.1</version>
131+
</dependency>
132+
<dependency>
133+
<groupId>commons-io</groupId>
134+
<artifactId>commons-io</artifactId>
135+
<version>2.2</version>
136+
</dependency>
137+
<dependency>
138+
<groupId>commons-logging</groupId>
139+
<artifactId>commons-logging</artifactId>
140+
<version>1.1.3</version>
141+
</dependency>
142+
<dependency>
143+
<groupId>org.apache.commons</groupId>
144+
<artifactId>commons-lang3</artifactId>
145+
<version>3.1</version>
146+
</dependency>
147+
148+
<dependency>
149+
<groupId>commons-collections</groupId>
150+
<artifactId>commons-collections</artifactId>
151+
<version>3.2.1</version>
152+
</dependency>
153+
<!-- @ResponseBody 转成json数据时要用到 -->
154+
<dependency>
155+
<groupId>com.fasterxml.jackson.core</groupId>
156+
<artifactId>jackson-databind</artifactId>
157+
<version>2.2.3</version>
158+
</dependency>
159+
</dependencies>
160+
161+
162+
163+
164+
<build>
165+
<finalName>cg</finalName>
166+
</build>
167+
</project>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package scut.legend.cg.controller;
2+
3+
import java.util.List;
4+
5+
import javax.annotation.Resource;
6+
7+
import org.apache.shiro.SecurityUtils;
8+
import org.apache.shiro.authc.IncorrectCredentialsException;
9+
import org.apache.shiro.authc.UnknownAccountException;
10+
import org.apache.shiro.authc.UsernamePasswordToken;
11+
import org.apache.shiro.authc.credential.PasswordService;
12+
import org.apache.shiro.session.Session;
13+
import org.apache.shiro.subject.Subject;
14+
import org.springframework.stereotype.Controller;
15+
import org.springframework.web.bind.annotation.PathVariable;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RequestMethod;
19+
import org.springframework.web.bind.annotation.RequestParam;
20+
import org.springframework.web.bind.annotation.ResponseBody;
21+
import org.springframework.web.servlet.ModelAndView;
22+
23+
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.PrivateKeyResolver;
24+
25+
import ch.qos.logback.core.net.LoginAuthenticator;
26+
import scut.legend.cg.po.User;
27+
import scut.legend.cg.service.UserService;
28+
29+
@Controller
30+
public class UserController {
31+
@Resource
32+
private UserService userService;
33+
@Resource
34+
private PasswordService passwordService;
35+
36+
// @RequestMapping(value="/user")
37+
// @ResponseBody
38+
// public User testUser(@RequestBody User user){
39+
// System.out.println(user);
40+
// return user;
41+
// }
42+
//
43+
@RequestMapping(value="/user/{id}")
44+
@ResponseBody
45+
public User testList(@PathVariable("id")Integer id){
46+
User user=userService.getUserById(id);
47+
return user;
48+
}
49+
50+
@RequestMapping(value="/login",method=RequestMethod.POST)
51+
@ResponseBody
52+
public String login(@RequestBody User user){
53+
UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword());
54+
try{
55+
SecurityUtils.getSubject().login(token);
56+
return "success";
57+
}catch(UnknownAccountException e){
58+
return "账号不存在";
59+
}catch(IncorrectCredentialsException e){
60+
return "密码错误";
61+
}catch (Exception e) {
62+
return "其他错误";
63+
}
64+
}
65+
66+
@RequestMapping(value="/login",method=RequestMethod.GET)
67+
public String login(){
68+
return "login";
69+
}
70+
71+
@RequestMapping(value="/unauthorized")
72+
public String unauthorized(){
73+
return "unauthorized";
74+
}
75+
76+
@RequestMapping(value="/encry")
77+
@ResponseBody
78+
public String encry(@RequestParam(value="password")String password){
79+
return passwordService.encryptPassword(password);
80+
}
81+
82+
@RequestMapping(value="/logout")
83+
public ModelAndView logout(){
84+
SecurityUtils.getSubject().logout();
85+
return new ModelAndView("login");
86+
}
87+
88+
@RequestMapping(value="/index")
89+
@ResponseBody
90+
public String index(){
91+
return "如果你看到这个页面,说明你已经登录,否则会跳到登录页面";
92+
}
93+
94+
@RequestMapping(value="/index2")
95+
@ResponseBody
96+
public String index2(){
97+
return "如果你看到这个页面,说明你已经登录或者选择了记住我选项";
98+
}
99+
100+
@RequestMapping(value="/user")
101+
@ResponseBody
102+
public String getUser(){
103+
return "role为user的用户才能看到这个页面";
104+
}
105+
106+
@RequestMapping(value="/admin")
107+
@ResponseBody
108+
public String getAdmin(){
109+
return "role为admin的用户才能看到这个页面";
110+
}
111+
112+
113+
@RequestMapping(value="/user/view")
114+
@ResponseBody
115+
public String viewUser(){
116+
return "拥有user:view权限的用户才能看到这个页面";
117+
}
118+
119+
@RequestMapping(value="/user/create")
120+
@ResponseBody
121+
public String createUser(){
122+
return "拥有user:create权限的用户才能看到这个页面";
123+
}
124+
125+
126+
127+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scut.legend.cg.controller.exception;
2+
3+
import org.apache.shiro.authz.UnauthorizedException;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.bind.annotation.ControllerAdvice;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
import org.springframework.web.bind.annotation.ResponseStatus;
8+
import org.springframework.web.context.request.NativeWebRequest;
9+
import org.springframework.web.servlet.ModelAndView;
10+
11+
@ControllerAdvice
12+
public class DefaultExceptionHandler {
13+
/**
14+
* 没有权限 异常
15+
*/
16+
@ExceptionHandler({UnauthorizedException.class})
17+
//@ResponseStatus(HttpStatus.UNAUTHORIZED)
18+
public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
19+
ModelAndView mv = new ModelAndView();
20+
mv.addObject("exception", e);
21+
mv.setViewName("unauthorized");
22+
return mv;
23+
}
24+
25+
}

0 commit comments

Comments
 (0)