Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengtianlai3
ioc_sixiang_license
Commits
e216ecce
Commit
e216ecce
authored
Jun 11, 2022
by
zengtianlai3
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
公网跨域测试
parent
c68f08a2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
28 deletions
+36
-28
JwtFilter.java
license/src/main/java/iot/sixiang/license/jwt/JwtFilter.java
+36
-28
No files found.
license/src/main/java/iot/sixiang/license/jwt/JwtFilter.java
View file @
e216ecce
...
@@ -14,9 +14,10 @@ import java.io.IOException;
...
@@ -14,9 +14,10 @@ import java.io.IOException;
import
java.util.Map
;
import
java.util.Map
;
@Slf4j
@Slf4j
@WebFilter
(
filterName
=
"jwtFilter"
,
urlPatterns
=
"/
iot_license/
*"
)
@WebFilter
(
filterName
=
"jwtFilter"
,
urlPatterns
=
"/*"
)
public
class
JwtFilter
implements
Filter
{
public
class
JwtFilter
implements
Filter
{
private
static
final
String
url1
=
"/login"
;
private
static
final
String
url2
=
"/resource"
;
@Override
@Override
public
void
init
(
FilterConfig
filterConfig
)
throws
ServletException
{
public
void
init
(
FilterConfig
filterConfig
)
throws
ServletException
{
...
@@ -28,45 +29,52 @@ public class JwtFilter implements Filter {
...
@@ -28,45 +29,52 @@ public class JwtFilter implements Filter {
public
void
doFilter
(
ServletRequest
servletRequest
,
ServletResponse
servletResponse
,
FilterChain
filterChain
)
throws
IOException
,
ServletException
{
public
void
doFilter
(
ServletRequest
servletRequest
,
ServletResponse
servletResponse
,
FilterChain
filterChain
)
throws
IOException
,
ServletException
{
final
HttpServletRequest
request
=
(
HttpServletRequest
)
servletRequest
;
final
HttpServletRequest
request
=
(
HttpServletRequest
)
servletRequest
;
final
HttpServletResponse
response
=
(
HttpServletResponse
)
servletResponse
;
final
HttpServletResponse
response
=
(
HttpServletResponse
)
servletResponse
;
response
.
setContentType
(
"text/html; charset=utf-8"
);
response
.
setContentType
(
"text/html; charset=utf-8"
);
//获取header里的token
response
.
setHeader
(
"Access-Control-Allow-Origin"
,
"*"
);
String
token
=
request
.
getHeader
(
"authorization"
);
response
.
addHeader
(
"Access-Control-Allow-Credentials"
,
"true"
);
//除了 OPTIONS请求以外, 其它请求应该被JWT检查
response
.
addHeader
(
"Access-Control-Allow-Methods"
,
"GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH"
);
if
(
"OPTIONS"
.
equals
(
request
.
getMethod
()))
{
if
(
"OPTIONS"
.
equals
(
request
.
getMethod
()))
{
response
.
setStatus
(
HttpServletResponse
.
SC_OK
);
response
.
setStatus
(
HttpServletResponse
.
SC_OK
);
}
String
token
=
request
.
getHeader
(
"authorization"
);
boolean
check
=
true
;
String
uri
=
request
.
getRequestURI
();
if
(
uri
.
contains
(
url1
)
||
uri
.
contains
(
url2
))
{
check
=
false
;
}
if
(!
check
)
{
filterChain
.
doFilter
(
request
,
response
);
filterChain
.
doFilter
(
request
,
response
);
}
else
{
return
;
if
(
token
==
null
)
{
String
resultStr
=
JSON
.
toJSONString
(
ResResult
.
fail
().
msg
(
"认证信息不能为空"
));
response
.
getWriter
().
write
(
resultStr
);
return
;
}
}
}
DecodedJWT
jwt
=
JwtUtil
.
verifyToken
(
token
);
if
(
token
==
null
)
{
if
(
jwt
==
null
){
String
resultStr
=
JSON
.
toJSONString
(
ResResult
.
fail
().
msg
(
"认证信息不能为空"
));
String
resultStr
=
JSON
.
toJSONString
(
ResResult
.
fail
().
msg
(
"认证信息非法"
));
response
.
getWriter
().
write
(
resultStr
);
response
.
getWriter
().
write
(
resultStr
);
return
;
return
;
}
else
{
}
else
{
Map
<
String
,
Claim
>
userData
=
jwt
.
getClaims
(
);
DecodedJWT
jwt
=
JwtUtil
.
verifyToken
(
token
);
if
(
userData
==
null
)
{
if
(
jwt
==
null
)
{
String
resultStr
=
JSON
.
toJSONString
(
ResResult
.
fail
().
msg
(
"认证信息非法"
));
String
resultStr
=
JSON
.
toJSONString
(
ResResult
.
fail
().
msg
(
"认证信息非法"
));
response
.
getWriter
().
write
(
resultStr
);
response
.
getWriter
().
write
(
resultStr
);
return
;
return
;
}
else
{
Map
<
String
,
Claim
>
userData
=
jwt
.
getClaims
();
if
(
userData
==
null
)
{
String
resultStr
=
JSON
.
toJSONString
(
ResResult
.
fail
().
msg
(
"认证信息非法"
));
response
.
getWriter
().
write
(
resultStr
);
return
;
}
String
userName
=
userData
.
get
(
"userName"
).
asString
();
String
password
=
userData
.
get
(
"password"
).
asString
();
//拦截器 拿到用户信息,放到request中
request
.
setAttribute
(
"userName"
,
userName
);
request
.
setAttribute
(
"password"
,
password
);
filterChain
.
doFilter
(
request
,
response
);
}
}
String
userName
=
userData
.
get
(
"userName"
).
asString
();
String
password
=
userData
.
get
(
"password"
).
asString
();
//拦截器 拿到用户信息,放到request中
request
.
setAttribute
(
"userName"
,
userName
);
request
.
setAttribute
(
"password"
,
password
);
filterChain
.
doFilter
(
servletRequest
,
servletResponse
);
}
}
}
}
@Override
@Override
...
...
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