Skip to content

Commit e7f352a

Browse files
committed
添加代码示例springboot-jwt
1 parent 4cc394a commit e7f352a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+5302
-0
lines changed

springboot-jwt/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 此为注释– 将被Git 忽略
2+
# /结尾表示是目录,忽略目录和目录下的所有件
3+
# /开头表示根目录,否则是.gitignore的相对目录
4+
# !开头表示反选
5+
.idea/
6+
target/
7+
*.iml
8+
*.ipr
9+
*.iws
10+
*.log
11+
.svn/
12+
.project
13+
rebel.xml
14+
.rebel-remote.xml.*
15+
swagger.json
16+
swagger.adoc
17+

springboot-jwt/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-jwt/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
## 简介
3+
4+
一般来讲,对于RESTful API都会有认证(Authentication)和授权(Authorization)过程,保证API的安全性。
5+
6+
采用TOKEN认证,这种方式也是再HTTP头中,使用Authorization: Bearer <token>,使用最广泛的TOKEN是JWT,通过签名过的TOKEN。
7+
8+
基于Shiro+JWT可实现Token认证方式
9+
10+
## 测试
11+
12+
启动应用后,先访问登录接口,使用参数用户名=admin/密码=12345678,拿到token后再访问其他接口。
13+
14+
## 许可证
15+
16+
Copyright (c) 2018 Xiong Neng
17+
18+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>
19+
20+

springboot-jwt/pom.xml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-jwt</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-jwt</name>
13+
<description>集成JWT实现接口权限认证</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
<exclusions>
33+
<exclusion>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-tomcat</artifactId>
36+
</exclusion>
37+
</exclusions>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-jetty</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.auth0</groupId>
45+
<artifactId>java-jwt</artifactId>
46+
<version>3.3.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
52+
<exclusions>
53+
<exclusion>
54+
<groupId>com.vaadin.external.google</groupId>
55+
<artifactId>android-json</artifactId>
56+
</exclusion>
57+
</exclusions>
58+
</dependency>
59+
<!-- shiro 权限控制 -->
60+
<dependency>
61+
<groupId>org.apache.shiro</groupId>
62+
<artifactId>shiro-spring</artifactId>
63+
<version>1.4.0</version>
64+
<exclusions>
65+
<exclusion>
66+
<artifactId>slf4j-api</artifactId>
67+
<groupId>org.slf4j</groupId>
68+
</exclusion>
69+
</exclusions>
70+
</dependency>
71+
<!-- shiro ehcache (shiro缓存)-->
72+
<dependency>
73+
<groupId>org.apache.shiro</groupId>
74+
<artifactId>shiro-ehcache</artifactId>
75+
<version>1.4.0</version>
76+
<exclusions>
77+
<exclusion>
78+
<artifactId>slf4j-api</artifactId>
79+
<groupId>org.slf4j</groupId>
80+
</exclusion>
81+
</exclusions>
82+
</dependency>
83+
84+
<dependency>
85+
<groupId>org.apache.commons</groupId>
86+
<artifactId>commons-lang3</artifactId>
87+
<version>3.7</version>
88+
</dependency>
89+
</dependencies>
90+
91+
<build>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-compiler-plugin</artifactId>
96+
<version>3.6.1</version>
97+
<configuration>
98+
<!--<proc>none</proc>-->
99+
<source>1.8</source>
100+
<target>1.8</target>
101+
</configuration>
102+
</plugin>
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-surefire-plugin</artifactId>
106+
<version>2.20</version>
107+
<configuration>
108+
<systemPropertyVariables>
109+
<swaggerOutputDir>${project.basedir}/src/main/resources/swagger</swaggerOutputDir>
110+
<asciiDocOutputDir>${project.basedir}/src/main/resources/swagger/swagger</asciiDocOutputDir>
111+
</systemPropertyVariables>
112+
<skip>true</skip>
113+
</configuration>
114+
</plugin>
115+
<plugin>
116+
<groupId>org.springframework.boot</groupId>
117+
<artifactId>spring-boot-maven-plugin</artifactId>
118+
<executions>
119+
</executions>
120+
</plugin>
121+
</plugins>
122+
123+
<resources>
124+
<resource>
125+
<directory>src/main/resources</directory>
126+
</resource>
127+
<resource>
128+
<directory>src/main/java</directory>
129+
<includes>
130+
<include>**/*.xml</include>
131+
</includes>
132+
</resource>
133+
</resources>
134+
</build>
135+
136+
</project>

springboot-jwt/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.jwt;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.xncoding.jwt.api;
2+
3+
import com.xncoding.jwt.api.model.BaseResponse;
4+
import org.apache.shiro.ShiroException;
5+
import org.apache.shiro.authz.UnauthorizedException;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.ResponseStatus;
9+
import org.springframework.web.bind.annotation.RestControllerAdvice;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
13+
/**
14+
* 注意这个统一异常处理器只对认证过的用户调用接口中的异常有作用,对AuthenticationException没有用
15+
*/
16+
@RestControllerAdvice
17+
public class ExceptionController {
18+
19+
// 捕捉shiro的异常
20+
@ResponseStatus(HttpStatus.UNAUTHORIZED)
21+
@ExceptionHandler(ShiroException.class)
22+
public BaseResponse handle401(ShiroException e) {
23+
return new BaseResponse(false, "shiro的异常", null);
24+
}
25+
26+
// 捕捉UnauthorizedException
27+
@ResponseStatus(HttpStatus.UNAUTHORIZED)
28+
@ExceptionHandler(UnauthorizedException.class)
29+
public BaseResponse handle401() {
30+
return new BaseResponse(false, "UnauthorizedException", null);
31+
}
32+
33+
// 捕捉其他所有异常
34+
@ExceptionHandler(Exception.class)
35+
@ResponseStatus(HttpStatus.BAD_REQUEST)
36+
public BaseResponse globalException(HttpServletRequest request, Throwable ex) {
37+
return new BaseResponse(false, "其他异常", null);
38+
}
39+
40+
private HttpStatus getStatus(HttpServletRequest request) {
41+
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
42+
if (statusCode == null) {
43+
return HttpStatus.INTERNAL_SERVER_ERROR;
44+
}
45+
return HttpStatus.valueOf(statusCode);
46+
}
47+
}
48+

0 commit comments

Comments
 (0)