Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
rx
Commits
f816078b
Commit
f816078b
authored
Aug 22, 2024
by
wujipeng
Browse files
根据设备id查询最新的一条设备上报信息
parent
2a1c27ab
Changes
11
Show whitespace changes
Inline
Side-by-side
pom.xml
View file @
f816078b
...
...
@@ -29,10 +29,10 @@
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-security
</artifactId>
</dependency>
<!--
<dependency>
-->
<!--
<groupId>org.springframework.boot</groupId>
-->
<!--
<artifactId>spring-boot-starter-security</artifactId>
-->
<!--
</dependency>
-->
<!--lombok工具-->
<dependency>
<groupId>
org.projectlombok
</groupId>
...
...
src/main/java/com/ruzhoukj/rx/common/Result.java
0 → 100644
View file @
f816078b
package
com.ruzhoukj.rx.common
;
/**
* @author 邬吉朋
*/
public
class
Result
{
private
Integer
code
;
private
String
msg
;
private
Object
data
;
public
Result
()
{
}
public
Result
(
Integer
code
,
String
msg
,
Object
data
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
this
.
data
=
data
;
}
public
Integer
getCode
()
{
return
code
;
}
public
void
setCode
(
Integer
code
)
{
this
.
code
=
code
;
}
public
String
getMsg
()
{
return
msg
;
}
public
void
setMsg
(
String
msg
)
{
this
.
msg
=
msg
;
}
public
Object
getData
()
{
return
data
;
}
public
void
setData
(
Object
data
)
{
this
.
data
=
data
;
}
//定义静态方法 帮助我们快速构造result对象
public
static
Result
success
(
Object
data
)
{
return
new
Result
(
1
,
"success"
,
data
);
}
public
static
Result
success
()
{
return
new
Result
(
1
,
"success"
,
null
);
}
public
static
Result
success
(
String
msg
)
{
return
new
Result
(
1
,
msg
,
null
);
}
}
src/main/java/com/ruzhoukj/rx/controller/EquipmentController.java
View file @
f816078b
package
com.ruzhoukj.rx.controller
;
import
com.ruzhoukj.rx.common.R
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
com.ruzhoukj.rx.common.Result
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.web.bind.annotation.GetMapping
;
...
...
@@ -15,7 +14,7 @@ import javax.annotation.Resource;
* @author wujipeng
* @version 1.0
*/
@RequestMapping
(
"/
api/
"
)
@RequestMapping
(
"/
equ
"
)
@RestController
public
class
EquipmentController
{
...
...
@@ -24,8 +23,8 @@ public class EquipmentController {
@ApiOperation
(
value
=
"根据设备id查询最新的一条设备上报信息"
)
@GetMapping
(
"/{deviceId}"
)
public
R
getEquipment
ByDeviceId
(
@PathVariable
String
deviceId
)
{
return
R
.
ok
(
);
public
R
esult
getDeviceData
ByDeviceId
(
@PathVariable
String
deviceId
)
{
return
R
esult
.
success
(
equipmentService
.
getDeviceDataByDeviceId
(
deviceId
)
);
}
}
src/main/java/com/ruzhoukj/rx/controller/UserController.java
View file @
f816078b
...
...
@@ -25,45 +25,45 @@ import java.util.HashMap;
@CrossOrigin
@RestController
public
class
UserController
{
@Resource
private
UserService
userService
;
@Resource
private
JwtUtil
jwtUtil
;
//
@Resource
//
private UserService userService;
//
@Resource
//
private JwtUtil jwtUtil;
private
static
final
String
PW_PATTERN
=
"^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$"
;
@PostMapping
(
"/Login"
)
@ApiOperation
(
value
=
"用户登录"
,
notes
=
"调用方法传递对象过来"
)
public
R
login
(
@RequestBody
User
user
,
HttpServletRequest
req
)
{
UserEntity
users
=
userService
.
login
(
user
.
getUserName
(),
user
.
getPassword
());
if
(
users
==
null
)
{
Integer
isValid
=
(
Integer
)
req
.
getSession
().
getAttribute
(
user
.
getUserName
());
if
(
isValid
==
null
)
{
req
.
getSession
().
setAttribute
(
user
.
getUserName
(),
1
);
return
R
.
error
(
405
,
"用户名或密码错误,还有4次机会!"
);
}
else
{
if
(
isValid
>=
5
)
{
QueryWrapper
<
UserEntity
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
eq
(
"accountid"
,
user
.
getUserName
());
UserEntity
account
=
userService
.
getOne
(
queryWrapper
);
account
.
setValid
(
0
);
userService
.
updateById
(
account
);
return
R
.
error
(
405
,
"用户已锁定!"
);
}
else
{
req
.
getSession
().
setAttribute
(
user
.
getUserName
(),
isValid
+
1
);
return
R
.
error
(
405
,
"用户名或密码错误,还有"
+
(
4
-
isValid
)
+
"次机会!"
);
}
}
}
if
(
users
.
getValid
()
==
0
)
{
return
R
.
error
(
405
,
"用户已停用,请联系管理员!"
);
}
String
token
=
jwtUtil
.
createJWT
(
users
.
getId
(),
users
.
getUserName
(),
users
.
getPassword
());
req
.
getSession
().
setAttribute
(
user
.
getUserName
(),
null
);
req
.
getSession
().
setAttribute
(
"captcha"
,
null
);
//
UserEntity users = userService.login(user.getUserName(), user.getPassword());
//
if (users == null) {
//
Integer isValid = (Integer) req.getSession().getAttribute(user.getUserName());
//
if (isValid == null) {
//
req.getSession().setAttribute(user.getUserName(), 1);
//
return R.error(405, "用户名或密码错误,还有4次机会!");
//
} else {
//
if (isValid >= 5) {
//
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
//
queryWrapper.eq("accountid", user.getUserName());
//
UserEntity account = userService.getOne(queryWrapper);
//
account.setValid(0);
//
userService.updateById(account);
//
return R.error(405, "用户已锁定!");
//
} else {
//
req.getSession().setAttribute(user.getUserName(), isValid + 1);
//
return R.error(405, "用户名或密码错误,还有" + (4 - isValid) + "次机会!");
//
}
//
}
//
//
}
//
if (users.getValid() == 0) {
//
return R.error(405, "用户已停用,请联系管理员!");
//
}
//
String token = jwtUtil.createJWT(users.getId(), users.getUserName(), users.getPassword());
//
req.getSession().setAttribute(user.getUserName(), null);
//
req.getSession().setAttribute("captcha", null);
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"token"
,
token
);
//
map.put("token", token);
map
.
put
(
"backUrl"
,
null
);
return
R
.
ok
(
1
,
"登录成功!"
,
map
,
true
,
null
);
}
...
...
src/main/java/com/ruzhoukj/rx/dao/DeviceDao.java
View file @
f816078b
package
com.ruzhoukj.rx.dao
;
import
com.ruzhoukj.rx.pojo.entity.DeviceEntity
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
org.springframework.data.mongodb.repository.MongoRepository
;
import
org.springframework.stereotype.Component
;
...
...
@@ -9,4 +10,6 @@ import org.springframework.stereotype.Component;
*/
@Component
public
interface
DeviceDao
extends
MongoRepository
<
DeviceEntity
,
String
>
{
DeviceVO
findTopByDeviceIdOrderByTimestampDesc
(
String
deviceId
);
}
src/main/java/com/ruzhoukj/rx/filter/JwtInterceptor.java
View file @
f816078b
...
...
@@ -16,41 +16,41 @@ import javax.servlet.http.HttpServletResponse;
/*
* 请求前统一拦截,验证token
* */
@Slf4j
@Component
//放到容器中
//
@Slf4j
//
@Component
public
class
JwtInterceptor
implements
HandlerInterceptor
{
@Resource
private
JwtUtil
jwtUtil
;
@Resource
private
UserService
userService
;
@Override
public
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
String
token
=
request
.
getHeader
(
"Authorization"
);
log
.
info
(
"用户token----------"
+
token
);
if
(
token
==
null
||
token
.
equals
(
""
))
{
throw
new
NullTokenException
();
}
else
{
String
s
=
request
.
getRequestURI
();
log
.
debug
(
"这是请求路径--------"
+
s
);
String
userId
=
(
String
)
jwtUtil
.
parseJWT
(
token
).
get
(
"jti"
);
request
.
setAttribute
(
"userId"
,
userId
);
UserEntity
user
=
userService
.
getUser
(
userId
);
String
newPass
=
null
;
if
(
user
!=
null
)
{
newPass
=
user
.
getPassword
();
}
if
(
newPass
!=
null
)
{
String
password
=
(
String
)
jwtUtil
.
parseJWT
(
token
).
get
(
"password"
);
if
(!
newPass
.
equals
(
password
))
{
throw
new
ContrastException
();
}
else
{
request
.
setAttribute
(
"userId"
,
userId
);
}
}
else
{
request
.
setAttribute
(
"userId"
,
userId
);
}
}
return
true
;
}
//
@Resource
//
private JwtUtil jwtUtil;
//
@Resource
//
private UserService userService;
//
//
@Override
//
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
String token = request.getHeader("Authorization");
//
log.info("用户token----------" + token);
//
if (token == null || token.equals("")) {
//
throw new NullTokenException();
//
} else {
//
String s = request.getRequestURI();
//
log.debug("这是请求路径--------" + s);
//
String userId = (String) jwtUtil.parseJWT(token).get("jti");
//
request.setAttribute("userId", userId);
//
UserEntity user = userService.getUser(userId);
//
String newPass = null;
//
if (user != null) {
//
newPass = user.getPassword();
//
}
//
if (newPass != null) {
//
String password = (String) jwtUtil.parseJWT(token).get("password");
//
if (!newPass.equals(password)) {
//
throw new ContrastException();
//
} else {
//
request.setAttribute("userId", userId);
//
}
//
} else {
//
request.setAttribute("userId", userId);
//
}
//
}
//
return true;
//
}
}
src/main/java/com/ruzhoukj/rx/service/EquipmentService.java
View file @
f816078b
...
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.rx.service;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
/**
* @author wujipeng
...
...
@@ -9,4 +10,9 @@ import com.ruzhoukj.rx.pojo.entity.EquipmentEntity;
*/
public
interface
EquipmentService
extends
IService
<
EquipmentEntity
>
{
/**
* @param deviceId 设备id
* @return DeviceVO
*/
DeviceVO
getDeviceDataByDeviceId
(
String
deviceId
);
}
src/main/java/com/ruzhoukj/rx/service/impl/EquipmentServiceImpl.java
View file @
f816078b
package
com.ruzhoukj.rx.service.impl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.ruzhoukj.rx.dao.DeviceDao
;
import
com.ruzhoukj.rx.dao.EquipmentDao
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
/**
* @author wujipeng
* @version 1.0
...
...
@@ -13,4 +17,12 @@ import org.springframework.stereotype.Service;
@Service
public
class
EquipmentServiceImpl
extends
ServiceImpl
<
EquipmentDao
,
EquipmentEntity
>
implements
EquipmentService
{
@Resource
DeviceDao
deviceDao
;
@Override
public
DeviceVO
getDeviceDataByDeviceId
(
String
deviceId
)
{
return
deviceDao
.
findTopByDeviceIdOrderByTimestampDesc
(
deviceId
);
}
}
src/main/java/com/ruzhoukj/rx/util/JwtUtil.java
View file @
f816078b
package
com.ruzhoukj.rx.util
;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.JwtBuilder
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.SignatureAlgorithm
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
//import io.jsonwebtoken.Claims;
//import io.jsonwebtoken.JwtBuilder;
//import io.jsonwebtoken.Jwts;
//import io.jsonwebtoken.SignatureAlgorithm;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.stereotype.Component;
import
cn.hutool.jwt.Claims
;
import
java.util.Date
;
/**
* @author qianmo
*/
@Component
@ConfigurationProperties
(
"jwt.config"
)
//从配置文件中取数据
//
@Component
//
@ConfigurationProperties("jwt.config")
public
class
JwtUtil
{
private
String
key
;
private
long
ttl
;
//一个小时
public
String
getKey
()
{
return
key
;
}
public
void
setKey
(
String
key
)
{
this
.
key
=
key
;
}
public
long
getTtl
()
{
return
ttl
;
}
public
void
setTtl
(
long
ttl
)
{
this
.
ttl
=
ttl
;
}
/**
* 生成JWT
*
* @param id
* @param subject
* @return
*/
public
String
createJWT
(
String
id
,
String
subject
,
String
password
)
{
long
nowMillis
=
System
.
currentTimeMillis
();
Date
now
=
new
Date
(
nowMillis
);
JwtBuilder
builder
=
Jwts
.
builder
().
setId
(
id
)
.
setSubject
(
subject
)
.
setIssuedAt
(
now
)
.
signWith
(
SignatureAlgorithm
.
HS256
,
key
).
claim
(
"password"
,
password
);
if
(
ttl
>
0
)
{
builder
.
setExpiration
(
new
Date
(
nowMillis
+
ttl
));
}
return
builder
.
compact
();
}
/**
* 解析JWT
* @param jwtStr
* @return
*/
public
Claims
parseJWT
(
String
jwtStr
){
return
Jwts
.
parser
()
.
setSigningKey
(
key
)
.
parseClaimsJws
(
jwtStr
)
.
getBody
();
}
//
//
private String key ;
//
//
private long ttl ;//一个小时
//
//
public String getKey() {
//
return key;
//
}
//
//
public void setKey(String key) {
//
this.key = key;
//
}
//
//
public long getTtl() {
//
return ttl;
//
}
//
//
public void setTtl(long ttl) {
//
this.ttl = ttl;
//
}
//
//
/**
//
* 生成JWT
//
*
//
* @param id
//
* @param subject
//
* @return
//
*/
//
public String createJWT(String id, String subject, String password) {
//
long nowMillis = System.currentTimeMillis();
//
Date now = new Date(nowMillis);
//
JwtBuilder builder = Jwts.builder().setId(id)
//
.setSubject(subject)
//
.setIssuedAt(now)
//
.signWith(SignatureAlgorithm.HS256, key).claim("password", password);
//
if (ttl > 0) {
//
builder.setExpiration( new Date( nowMillis + ttl));
//
}
//
return builder.compact();
//
}
//
//
/**
//
* 解析JWT
//
* @param jwtStr
//
* @return
//
*/
//
public Claims parseJWT(String jwtStr){
//
return Jwts.parser()
//
.setSigningKey(key)
//
.parseClaimsJws(jwtStr)
//
.getBody();
//
}
}
src/main/resources/application.yml
View file @
f816078b
...
...
@@ -16,6 +16,12 @@ spring:
host
:
192.168.10.3
authentication-database
:
admin
mybatis-plus
:
mapper-locations
:
classpath:/mapper/*.xml
configuration
:
call-setters-on-nulls
:
true
#列为空是也返回字段 map
return-instance-for-empty-row
:
true
#所有都为空时返回一个空实例 map
log-impl
:
org.apache.ibatis.logging.stdout.StdOutImpl
mqtt
:
host
:
tcp://119.3.152.139:1883
#MQTT-服务器连接地址,如果有多个,用逗号隔开,如:tcp://127.0.0.1:61613,tcp://192.168.2.133:61613
...
...
src/main/resources/mapper/com/ruzhoukj/rx/dao/DeviceDao.xml
0 → 100644
View file @
f816078b
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper
namespace=
"com.ruzhoukj.rx.dao.DeviceDao"
>
<select
id=
"findTopByDeviceIdOrderByTimestampDesc"
resultType=
"com.ruzhoukj.rx.pojo.vo.DeviceVO"
>
SELECT deviceId
FROM gsd
WHERE deviceId = #{deviceId}
ORDER BY timestamp DESC
LIMIT 1
</select>
</mapper>
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