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
5af86da3
Commit
5af86da3
authored
Jun 13, 2022
by
zengtianlai3
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加ThreadLocal
parent
dd9cc4b7
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
108 additions
and
20 deletions
+108
-20
CorsConfig.java
.../src/main/java/iot/sixiang/license/config/CorsConfig.java
+13
-4
AlarmController.java
.../java/iot/sixiang/license/controller/AlarmController.java
+6
-3
LoginController.java
.../java/iot/sixiang/license/controller/LoginController.java
+6
-8
AuthenticationInterceptor.java
...va/iot/sixiang/license/jwt/AuthenticationInterceptor.java
+28
-0
JwtFilter.java
license/src/main/java/iot/sixiang/license/jwt/JwtFilter.java
+8
-4
JwtUtil.java
license/src/main/java/iot/sixiang/license/jwt/JwtUtil.java
+1
-0
LoginUser.java
license/src/main/java/iot/sixiang/license/jwt/LoginUser.java
+5
-1
UserUtils.java
license/src/main/java/iot/sixiang/license/jwt/UserUtils.java
+41
-0
No files found.
license/src/main/java/iot/sixiang/license/config/CorsConfig.java
View file @
5af86da3
package
iot
.
sixiang
.
license
.
config
;
import
iot.sixiang.license.jwt.AuthenticationInterceptor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
org.springframework.web.servlet.config.annotation.*
;
@Configuration
@EnableWebMvc
public
class
CorsConfig
implements
WebMvcConfigurer
{
@Autowired
AuthenticationInterceptor
authenticationInterceptor
;
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
registry
.
addMapping
(
"/**"
)
...
...
@@ -23,6 +25,13 @@ public class CorsConfig implements WebMvcConfigurer {
@Override
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
// TODO Auto-generated method stub
}
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
registry
.
addInterceptor
(
authenticationInterceptor
)
.
addPathPatterns
(
"/**"
);
}
}
license/src/main/java/iot/sixiang/license/controller/AlarmController.java
View file @
5af86da3
package
iot
.
sixiang
.
license
.
controller
;
import
iot.sixiang.license.jwt.LoginUser
;
import
iot.sixiang.license.jwt.UserUtils
;
import
iot.sixiang.license.model.ResResult
;
import
iot.sixiang.license.model.vo.AlarmVo
;
import
iot.sixiang.license.service.AlarmService
;
...
...
@@ -25,10 +27,11 @@ public class AlarmController {
private
AlarmService
alarmService
;
@GetMapping
(
"list"
)
// public ResResult getAlarmList(@RequestParam(value = "userId", defaultValue = "0") int userId) {
public
ResResult
getAlarmList
()
{
int
userId
=
2147483647
;
List
<
AlarmVo
>
alarmList
=
alarmService
.
getAlarmList
(
userId
);
String
userId
=
UserUtils
.
getLoginUserId
();
int
Id
=
Integer
.
valueOf
(
userId
);
List
<
AlarmVo
>
alarmList
=
alarmService
.
getAlarmList
(
Id
);
return
ResResult
.
success
().
record
(
alarmList
);
}
}
...
...
license/src/main/java/iot/sixiang/license/controller/LoginController.java
View file @
5af86da3
...
...
@@ -2,6 +2,7 @@ package iot.sixiang.license.controller;
import
iot.sixiang.license.jwt.JwtUtil
;
import
iot.sixiang.license.jwt.LoginUser
;
import
iot.sixiang.license.jwt.UserUtils
;
import
iot.sixiang.license.model.ResResult
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.GetMapping
;
...
...
@@ -21,11 +22,11 @@ import java.util.Map;
public
class
LoginController
{
//模拟数据库
static
Map
<
Integer
,
LoginUser
>
userMap
=
new
HashMap
<>();
static
Map
<
String
,
LoginUser
>
userMap
=
new
HashMap
<>();
static
{
LoginUser
user1
=
new
LoginUser
(
"root"
,
"123456"
);
userMap
.
put
(
1
,
user1
);
LoginUser
user1
=
new
LoginUser
(
"
2147483647"
,
"
root"
,
"123456"
);
userMap
.
put
(
"2147483647"
,
user1
);
}
/**
...
...
@@ -34,13 +35,10 @@ public class LoginController {
@GetMapping
(
"login"
)
public
ResResult
login
(
@RequestParam
(
"userName"
)
String
userName
,
@RequestParam
(
"password"
)
String
password
)
{
// @RequestBody JSONObject jsonObject
LoginUser
user
=
new
LoginUser
();
user
.
setUserName
(
userName
);
user
.
setPassword
(
password
);
for
(
LoginUser
dbUser
:
userMap
.
values
())
{
if
(
dbUser
.
getUserName
().
equals
(
user
.
getUserName
())
&&
dbUser
.
getPassword
().
equals
(
user
.
getPassword
()
))
{
if
(
dbUser
.
getUserName
().
equals
(
user
Name
)
&&
dbUser
.
getPassword
().
equals
(
password
))
{
log
.
info
(
"登录成功!生成token!"
);
String
token
=
JwtUtil
.
createToken
(
dbUser
);
Map
<
String
,
String
>
map
=
new
HashMap
<>();
...
...
license/src/main/java/iot/sixiang/license/jwt/AuthenticationInterceptor.java
0 → 100644
View file @
5af86da3
package
iot
.
sixiang
.
license
.
jwt
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* Title: AuthenticationInterceptor
* Description: TODO
*
* @author m33
* @version V1.0
* @date 2022-06-13
*/
@Slf4j
@Component
public
class
AuthenticationInterceptor
implements
HandlerInterceptor
{
@Override
public
void
afterCompletion
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
Exception
ex
)
throws
Exception
{
UserUtils
.
removeUser
();
}
}
\ No newline at end of file
license/src/main/java/iot/sixiang/license/jwt/JwtFilter.java
View file @
5af86da3
...
...
@@ -65,12 +65,16 @@ public class JwtFilter implements Filter {
response
.
getWriter
().
write
(
resultStr
);
return
;
}
String
userId
=
userData
.
get
(
"userId"
).
asString
();
String
userName
=
userData
.
get
(
"userName"
).
asString
();
String
password
=
userData
.
get
(
"password"
).
asString
();
//拦截器 拿到用户信息,放到request中
request
.
setAttribute
(
"userName"
,
userName
);
request
.
setAttribute
(
"password"
,
password
);
// //拦截器 拿到用户信息,放到request中
// request.setAttribute("userName", userName);
// request.setAttribute("password", password);
LoginUser
loginUser
=
new
LoginUser
(
userId
,
userName
,
password
);
UserUtils
.
setLoginUser
(
loginUser
);
filterChain
.
doFilter
(
request
,
response
);
}
}
...
...
license/src/main/java/iot/sixiang/license/jwt/JwtUtil.java
View file @
5af86da3
...
...
@@ -35,6 +35,7 @@ public class JwtUtil {
String
token
=
JWT
.
create
()
.
withHeader
(
map
)
//添加头部
//可以把数据存在claim中
.
withClaim
(
"userId"
,
user
.
getUserId
())
.
withClaim
(
"userName"
,
user
.
getUserName
())
.
withClaim
(
"password"
,
user
.
getPassword
())
.
withExpiresAt
(
expireDate
)
//超时设置,设置过期的日期
...
...
license/src/main/java/iot/sixiang/license/jwt/LoginUser.java
View file @
5af86da3
...
...
@@ -4,14 +4,18 @@ import lombok.Data;
@Data
public
class
LoginUser
{
private
String
userId
;
private
String
userName
;
private
String
password
;
public
LoginUser
()
{
}
public
LoginUser
(
String
userName
,
String
password
)
{
public
LoginUser
(
String
userId
,
String
userName
,
String
password
)
{
this
.
userId
=
userId
;
this
.
userName
=
userName
;
this
.
password
=
password
;
}
}
license/src/main/java/iot/sixiang/license/jwt/UserUtils.java
0 → 100644
View file @
5af86da3
package
iot
.
sixiang
.
license
.
jwt
;
/**
* 存储/获取当前线程的用户信息工具类
*/
public
abstract
class
UserUtils
{
//线程变量,存放user实体类信息,即使是静态的与其他线程也是隔离的
private
static
ThreadLocal
<
LoginUser
>
userThreadLocal
=
new
ThreadLocal
<
LoginUser
>();
//从当前线程变量中获取用户信息
public
static
LoginUser
getLoginUser
()
{
LoginUser
user
=
userThreadLocal
.
get
();
return
user
;
}
/**
* 获取当前登录用户的ID
* 未登录返回null
*
* @return
*/
public
static
String
getLoginUserId
()
{
LoginUser
user
=
userThreadLocal
.
get
();
if
(
user
!=
null
&&
user
.
getUserId
()
!=
null
)
{
return
user
.
getUserId
();
}
return
null
;
}
//为当前的线程变量赋值上用户信息
public
static
void
setLoginUser
(
LoginUser
user
)
{
userThreadLocal
.
set
(
user
);
}
//清除线程变量
public
static
void
removeUser
()
{
userThreadLocal
.
remove
();
}
}
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