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 @@
...
@@ -29,10 +29,10 @@
<artifactId>
spring-boot-starter-test
</artifactId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<!--
<dependency>
-->
<groupId>
org.springframework.boot
</groupId>
<!--
<groupId>org.springframework.boot</groupId>
-->
<artifactId>
spring-boot-starter-security
</artifactId>
<!--
<artifactId>spring-boot-starter-security</artifactId>
-->
</dependency>
<!--
</dependency>
-->
<!--lombok工具-->
<!--lombok工具-->
<dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<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
;
package
com.ruzhoukj.rx.controller
;
import
com.ruzhoukj.rx.common.R
;
import
com.ruzhoukj.rx.common.Result
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
...
@@ -15,7 +14,7 @@ import javax.annotation.Resource;
...
@@ -15,7 +14,7 @@ import javax.annotation.Resource;
* @author wujipeng
* @author wujipeng
* @version 1.0
* @version 1.0
*/
*/
@RequestMapping
(
"/
api/
"
)
@RequestMapping
(
"/
equ
"
)
@RestController
@RestController
public
class
EquipmentController
{
public
class
EquipmentController
{
...
@@ -24,8 +23,8 @@ public class EquipmentController {
...
@@ -24,8 +23,8 @@ public class EquipmentController {
@ApiOperation
(
value
=
"根据设备id查询最新的一条设备上报信息"
)
@ApiOperation
(
value
=
"根据设备id查询最新的一条设备上报信息"
)
@GetMapping
(
"/{deviceId}"
)
@GetMapping
(
"/{deviceId}"
)
public
R
getEquipment
ByDeviceId
(
@PathVariable
String
deviceId
)
{
public
R
esult
getDeviceData
ByDeviceId
(
@PathVariable
String
deviceId
)
{
return
R
.
ok
(
);
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;
...
@@ -25,45 +25,45 @@ import java.util.HashMap;
@CrossOrigin
@CrossOrigin
@RestController
@RestController
public
class
UserController
{
public
class
UserController
{
@Resource
//
@Resource
private
UserService
userService
;
//
private UserService userService;
@Resource
//
@Resource
private
JwtUtil
jwtUtil
;
//
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,}$"
;
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"
)
@PostMapping
(
"/Login"
)
@ApiOperation
(
value
=
"用户登录"
,
notes
=
"调用方法传递对象过来"
)
@ApiOperation
(
value
=
"用户登录"
,
notes
=
"调用方法传递对象过来"
)
public
R
login
(
@RequestBody
User
user
,
HttpServletRequest
req
)
{
public
R
login
(
@RequestBody
User
user
,
HttpServletRequest
req
)
{
UserEntity
users
=
userService
.
login
(
user
.
getUserName
(),
user
.
getPassword
());
//
UserEntity users = userService.login(user.getUserName(), user.getPassword());
if
(
users
==
null
)
{
//
if (users == null) {
Integer
isValid
=
(
Integer
)
req
.
getSession
().
getAttribute
(
user
.
getUserName
());
//
Integer isValid = (Integer) req.getSession().getAttribute(user.getUserName());
if
(
isValid
==
null
)
{
//
if (isValid == null) {
req
.
getSession
().
setAttribute
(
user
.
getUserName
(),
1
);
//
req.getSession().setAttribute(user.getUserName(), 1);
return
R
.
error
(
405
,
"用户名或密码错误,还有4次机会!"
);
//
return R.error(405, "用户名或密码错误,还有4次机会!");
}
else
{
//
} else {
if
(
isValid
>=
5
)
{
//
if (isValid >= 5) {
QueryWrapper
<
UserEntity
>
queryWrapper
=
new
QueryWrapper
<>();
//
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
queryWrapper
.
eq
(
"accountid"
,
user
.
getUserName
());
//
queryWrapper.eq("accountid", user.getUserName());
UserEntity
account
=
userService
.
getOne
(
queryWrapper
);
//
UserEntity account = userService.getOne(queryWrapper);
account
.
setValid
(
0
);
//
account.setValid(0);
userService
.
updateById
(
account
);
//
userService.updateById(account);
return
R
.
error
(
405
,
"用户已锁定!"
);
//
return R.error(405, "用户已锁定!");
}
else
{
//
} else {
req
.
getSession
().
setAttribute
(
user
.
getUserName
(),
isValid
+
1
);
//
req.getSession().setAttribute(user.getUserName(), isValid + 1);
return
R
.
error
(
405
,
"用户名或密码错误,还有"
+
(
4
-
isValid
)
+
"次机会!"
);
//
return R.error(405, "用户名或密码错误,还有" + (4 - isValid) + "次机会!");
}
//
}
}
//
}
//
}
//
}
if
(
users
.
getValid
()
==
0
)
{
//
if (users.getValid() == 0) {
return
R
.
error
(
405
,
"用户已停用,请联系管理员!"
);
//
return R.error(405, "用户已停用,请联系管理员!");
}
//
}
String
token
=
jwtUtil
.
createJWT
(
users
.
getId
(),
users
.
getUserName
(),
users
.
getPassword
());
//
String token = jwtUtil.createJWT(users.getId(), users.getUserName(), users.getPassword());
req
.
getSession
().
setAttribute
(
user
.
getUserName
(),
null
);
//
req.getSession().setAttribute(user.getUserName(), null);
req
.
getSession
().
setAttribute
(
"captcha"
,
null
);
//
req.getSession().setAttribute("captcha", null);
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"token"
,
token
);
//
map.put("token", token);
map
.
put
(
"backUrl"
,
null
);
map
.
put
(
"backUrl"
,
null
);
return
R
.
ok
(
1
,
"登录成功!"
,
map
,
true
,
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
;
package
com.ruzhoukj.rx.dao
;
import
com.ruzhoukj.rx.pojo.entity.DeviceEntity
;
import
com.ruzhoukj.rx.pojo.entity.DeviceEntity
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
org.springframework.data.mongodb.repository.MongoRepository
;
import
org.springframework.data.mongodb.repository.MongoRepository
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -9,4 +10,6 @@ import org.springframework.stereotype.Component;
...
@@ -9,4 +10,6 @@ import org.springframework.stereotype.Component;
*/
*/
@Component
@Component
public
interface
DeviceDao
extends
MongoRepository
<
DeviceEntity
,
String
>
{
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;
...
@@ -16,41 +16,41 @@ import javax.servlet.http.HttpServletResponse;
/*
/*
* 请求前统一拦截,验证token
* 请求前统一拦截,验证token
* */
* */
@Slf4j
//
@Slf4j
@Component
//放到容器中
//
@Component
public
class
JwtInterceptor
implements
HandlerInterceptor
{
public
class
JwtInterceptor
implements
HandlerInterceptor
{
@Resource
//
@Resource
private
JwtUtil
jwtUtil
;
//
private JwtUtil jwtUtil;
@Resource
//
@Resource
private
UserService
userService
;
//
private UserService userService;
//
@Override
//
@Override
public
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
//
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String
token
=
request
.
getHeader
(
"Authorization"
);
//
String token = request.getHeader("Authorization");
log
.
info
(
"用户token----------"
+
token
);
//
log.info("用户token----------" + token);
if
(
token
==
null
||
token
.
equals
(
""
))
{
//
if (token == null || token.equals("")) {
throw
new
NullTokenException
();
//
throw new NullTokenException();
}
else
{
//
} else {
String
s
=
request
.
getRequestURI
();
//
String s = request.getRequestURI();
log
.
debug
(
"这是请求路径--------"
+
s
);
//
log.debug("这是请求路径--------" + s);
String
userId
=
(
String
)
jwtUtil
.
parseJWT
(
token
).
get
(
"jti"
);
//
String userId = (String) jwtUtil.parseJWT(token).get("jti");
request
.
setAttribute
(
"userId"
,
userId
);
//
request.setAttribute("userId", userId);
UserEntity
user
=
userService
.
getUser
(
userId
);
//
UserEntity user = userService.getUser(userId);
String
newPass
=
null
;
//
String newPass = null;
if
(
user
!=
null
)
{
//
if (user != null) {
newPass
=
user
.
getPassword
();
//
newPass = user.getPassword();
}
//
}
if
(
newPass
!=
null
)
{
//
if (newPass != null) {
String
password
=
(
String
)
jwtUtil
.
parseJWT
(
token
).
get
(
"password"
);
//
String password = (String) jwtUtil.parseJWT(token).get("password");
if
(!
newPass
.
equals
(
password
))
{
//
if (!newPass.equals(password)) {
throw
new
ContrastException
();
//
throw new ContrastException();
}
else
{
//
} else {
request
.
setAttribute
(
"userId"
,
userId
);
//
request.setAttribute("userId", userId);
}
//
}
}
else
{
//
} else {
request
.
setAttribute
(
"userId"
,
userId
);
//
request.setAttribute("userId", userId);
}
//
}
}
//
}
return
true
;
//
return true;
}
//
}
}
}
src/main/java/com/ruzhoukj/rx/service/EquipmentService.java
View file @
f816078b
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.rx.service;
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.rx.service;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
/**
/**
* @author wujipeng
* @author wujipeng
...
@@ -9,4 +10,9 @@ import com.ruzhoukj.rx.pojo.entity.EquipmentEntity;
...
@@ -9,4 +10,9 @@ import com.ruzhoukj.rx.pojo.entity.EquipmentEntity;
*/
*/
public
interface
EquipmentService
extends
IService
<
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
;
package
com.ruzhoukj.rx.service.impl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.ruzhoukj.rx.dao.DeviceDao
;
import
com.ruzhoukj.rx.dao.EquipmentDao
;
import
com.ruzhoukj.rx.dao.EquipmentDao
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
/**
/**
* @author wujipeng
* @author wujipeng
* @version 1.0
* @version 1.0
...
@@ -13,4 +17,12 @@ import org.springframework.stereotype.Service;
...
@@ -13,4 +17,12 @@ import org.springframework.stereotype.Service;
@Service
@Service
public
class
EquipmentServiceImpl
public
class
EquipmentServiceImpl
extends
ServiceImpl
<
EquipmentDao
,
EquipmentEntity
>
implements
EquipmentService
{
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
;
package
com.ruzhoukj.rx.util
;
import
io.jsonwebtoken.Claims
;
//import io.jsonwebtoken.Claims;
import
io.jsonwebtoken.JwtBuilder
;
//import io.jsonwebtoken.JwtBuilder;
import
io.jsonwebtoken.Jwts
;
//import io.jsonwebtoken.Jwts;
import
io.jsonwebtoken.SignatureAlgorithm
;
//import io.jsonwebtoken.SignatureAlgorithm;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
//import org.springframework.boot.context.properties.ConfigurationProperties;
import
org.springframework.stereotype.Component
;
//import org.springframework.stereotype.Component;
import
cn.hutool.jwt.Claims
;
import
java.util.Date
;
import
java.util.Date
;
/**
/**
* @author qianmo
* @author qianmo
*/
*/
@Component
//
@Component
@ConfigurationProperties
(
"jwt.config"
)
//从配置文件中取数据
//
@ConfigurationProperties("jwt.config")
public
class
JwtUtil
{
public
class
JwtUtil
{
//
private
String
key
;
//
private String key ;
//
private
long
ttl
;
//一个小时
//
private long ttl ;//一个小时
//
public
String
getKey
()
{
//
public String getKey() {
return
key
;
//
return key;
}
//
}
//
public
void
setKey
(
String
key
)
{
//
public void setKey(String key) {
this
.
key
=
key
;
//
this.key = key;
}
//
}
//
public
long
getTtl
()
{
//
public long getTtl() {
return
ttl
;
//
return ttl;
}
//
}
//
public
void
setTtl
(
long
ttl
)
{
//
public void setTtl(long ttl) {
this
.
ttl
=
ttl
;
//
this.ttl = ttl;
}
//
}
//
/**
//
/**
* 生成JWT
//
* 生成JWT
*
//
*
* @param id
//
* @param id
* @param subject
//
* @param subject
* @return
//
* @return
*/
//
*/
public
String
createJWT
(
String
id
,
String
subject
,
String
password
)
{
//
public String createJWT(String id, String subject, String password) {
long
nowMillis
=
System
.
currentTimeMillis
();
//
long nowMillis = System.currentTimeMillis();
Date
now
=
new
Date
(
nowMillis
);
//
Date now = new Date(nowMillis);
JwtBuilder
builder
=
Jwts
.
builder
().
setId
(
id
)
//
JwtBuilder builder = Jwts.builder().setId(id)
.
setSubject
(
subject
)
//
.setSubject(subject)
.
setIssuedAt
(
now
)
//
.setIssuedAt(now)
.
signWith
(
SignatureAlgorithm
.
HS256
,
key
).
claim
(
"password"
,
password
);
//
.signWith(SignatureAlgorithm.HS256, key).claim("password", password);
if
(
ttl
>
0
)
{
//
if (ttl > 0) {
builder
.
setExpiration
(
new
Date
(
nowMillis
+
ttl
));
//
builder.setExpiration( new Date( nowMillis + ttl));
}
//
}
return
builder
.
compact
();
//
return builder.compact();
}
//
}
//
/**
//
/**
* 解析JWT
//
* 解析JWT
* @param jwtStr
//
* @param jwtStr
* @return
//
* @return
*/
//
*/
public
Claims
parseJWT
(
String
jwtStr
){
//
public Claims parseJWT(String jwtStr){
return
Jwts
.
parser
()
//
return Jwts.parser()
.
setSigningKey
(
key
)
//
.setSigningKey(key)
.
parseClaimsJws
(
jwtStr
)
//
.parseClaimsJws(jwtStr)
.
getBody
();
//
.getBody();
}
//
}
}
}
src/main/resources/application.yml
View file @
f816078b
...
@@ -16,6 +16,12 @@ spring:
...
@@ -16,6 +16,12 @@ spring:
host
:
192.168.10.3
host
:
192.168.10.3
authentication-database
:
admin
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
:
mqtt
:
host
:
tcp://119.3.152.139:1883
#MQTT-服务器连接地址,如果有多个,用逗号隔开,如:tcp://127.0.0.1:61613,tcp://192.168.2.133:61613
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