Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
7ac9c3c1
Commit
7ac9c3c1
authored
Aug 23, 2017
by
stylefeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善验证token失效的方法
parent
4f688579
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
12 deletions
+18
-12
guns-rest/src/main/java/com/stylefeng/guns/rest/auth/JwtTokenUtil.java
+11
-0
guns-rest/src/main/java/com/stylefeng/guns/rest/filter/JwtAuthenticationTokenFilter.java
+7
-12
No files found.
guns-rest/src/main/java/com/stylefeng/guns/rest/auth/JwtTokenUtil.java
View file @
7ac9c3c1
...
...
@@ -2,6 +2,7 @@ package com.stylefeng.guns.rest.auth;
import
com.stylefeng.guns.rest.config.properties.JwtProperties
;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.JwtException
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.SignatureAlgorithm
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -52,6 +53,16 @@ public class JwtTokenUtil implements Serializable {
return
claimsResolver
.
apply
(
claims
);
}
public
Boolean
validateToken
(
String
token
)
{
try
{
//判断是否能解析出token
Jwts
.
parser
().
setSigningKey
(
jwtProperties
.
getSecret
()).
parseClaimsJws
(
token
).
getBody
();
return
true
;
}
catch
(
JwtException
e
)
{
return
false
;
}
}
private
Claims
getAllClaimsFromToken
(
String
token
)
{
return
Jwts
.
parser
()
.
setSigningKey
(
jwtProperties
.
getSecret
())
...
...
guns-rest/src/main/java/com/stylefeng/guns/rest/filter/JwtAuthenticationTokenFilter.java
View file @
7ac9c3c1
...
...
@@ -24,28 +24,23 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
private
JwtProperties
jwtProperties
;
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
chain
)
throws
ServletException
,
IOException
{
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
if
(
request
.
getServletPath
().
equals
(
"/"
+
jwtProperties
.
getAuthPath
()))
{
chain
.
doFilter
(
request
,
response
);
return
;
}
final
String
requestHeader
=
request
.
getHeader
(
jwtProperties
.
getHeader
());
String
username
=
null
;
String
authToken
=
null
;
if
(
requestHeader
!=
null
&&
requestHeader
.
startsWith
(
"Bearer "
))
{
authToken
=
requestHeader
.
substring
(
7
);
try
{
username
=
jwtTokenUtil
.
getUsernameFromToken
(
authToken
);
}
catch
(
IllegalArgumentException
e
)
{
logger
.
error
(
"an error occured during getting username from token"
,
e
);
return
;
boolean
flag
=
jwtTokenUtil
.
validateToken
(
authToken
);
if
(!
flag
)
{
logger
.
error
(
"token验证错误"
);
throw
new
RuntimeException
(
"token验证错误"
);
}
}
else
{
logger
.
warn
(
"
couldn't find bearer string, will ignore the
header"
);
return
;
logger
.
warn
(
"
错误的
header"
);
throw
new
RuntimeException
(
"错误的header"
)
;
}
chain
.
doFilter
(
request
,
response
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment