parent
aad457d225
commit
0b609c9787
@ -0,0 +1,24 @@
|
||||
package com.xht.springboot.Config;
|
||||
|
||||
|
||||
import com.xht.springboot.Interceptor.LoginInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class InterceptorConfig implements WebMvcConfigurer {
|
||||
@Autowired
|
||||
private LoginInterceptor loginInterceptor;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry
|
||||
.addInterceptor(loginInterceptor)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/user/login");
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.xht.springboot.Control;
|
||||
|
||||
|
||||
import com.xht.springboot.Entity.CancerInformation;
|
||||
import com.xht.springboot.Service.InformationQueryService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/information")
|
||||
public class InformationQueryController {
|
||||
@Autowired
|
||||
InformationQueryService informationQueryService;
|
||||
|
||||
@RequestMapping("/cancer")
|
||||
@ResponseBody
|
||||
public List<CancerInformation> getCancerInformation(String token){
|
||||
return informationQueryService.queryCancerInformation(token);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.xht.springboot.Dao;
|
||||
|
||||
|
||||
import com.xht.springboot.Entity.CancerInformation;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface InformationQueryMapper {
|
||||
public List<CancerInformation> queryCancerInformation(String token);
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.xht.springboot.Entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Data
|
||||
@ToString
|
||||
public class CancerInformation {
|
||||
private int id;
|
||||
private String name;
|
||||
private String impact;
|
||||
private String summary;
|
||||
private String symptom;
|
||||
private String factor;
|
||||
private String judge;
|
||||
private String heal;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.xht.springboot.Interceptor;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
@Component
|
||||
public class LoginInterceptor implements HandlerInterceptor {
|
||||
@Autowired
|
||||
RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
System.out.println(request.getRequestURI());
|
||||
String token = request.getHeader("token");
|
||||
String result = (String) redisTemplate.opsForValue().get(token);
|
||||
if(result==null)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.xht.springboot.Service;
|
||||
|
||||
|
||||
import com.xht.springboot.Dao.InformationQueryMapper;
|
||||
import com.xht.springboot.Entity.CancerInformation;
|
||||
import com.xht.springboot.Utils.FuncInterFaces.QueryFunctionForDB;
|
||||
import com.xht.springboot.Utils.RedisAndDbOpsUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class InformationQueryService {
|
||||
@Autowired
|
||||
InformationQueryMapper informationQueryMapper;
|
||||
@Autowired
|
||||
RedisAndDbOpsUtils redisAndDbOpsUtils;
|
||||
String cancerInformationCacheName = "cancerInformationByToken::";
|
||||
String cancerInformationLockName = "cancerInformationByTokenLock";
|
||||
|
||||
|
||||
public List<CancerInformation> queryCancerInformation(String token){
|
||||
QueryFunctionForDB<List<CancerInformation>> queryFunctionForDB = new QueryFunctionForDB<List<CancerInformation>>() {
|
||||
@Override
|
||||
public List<CancerInformation> queryForDB(String token) {
|
||||
List<CancerInformation> results = informationQueryMapper.queryCancerInformation(token);
|
||||
if(results.size()==0)
|
||||
return null;
|
||||
else
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
return redisAndDbOpsUtils.queryNoFilter(cancerInformationCacheName,token,cancerInformationLockName,queryFunctionForDB,token);
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package com.xht.springboot.Service;
|
||||
|
||||
|
||||
import com.xht.springboot.Dao.LoginAndRegisterMapper;
|
||||
import com.xht.springboot.Entity.UserLogin;
|
||||
import com.xht.springboot.Tip.RequestResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class LoginAndRegisterService {
|
||||
@Autowired
|
||||
LoginAndRegisterMapper loginAndRegisterMapper;
|
||||
@Autowired
|
||||
RequestResult requestResult;
|
||||
@Autowired
|
||||
RedisTemplate redisTemplate;
|
||||
|
||||
public RequestResult findIsRepeat(UserLogin userLogin){
|
||||
List<UserLogin> result = loginAndRegisterMapper.findIsRepeat(userLogin);
|
||||
RequestResult requestResult = new RequestResult();
|
||||
|
||||
if(result.size()==0){
|
||||
requestResult.status = RequestResult.OK;
|
||||
requestResult.message = RequestResult.USER_NOT_REPEAT;
|
||||
}else{
|
||||
requestResult.status = RequestResult.FAIL;
|
||||
requestResult.message = RequestResult.USER_REPEAT;
|
||||
}
|
||||
|
||||
return requestResult;
|
||||
}
|
||||
|
||||
public RequestResult register(UserLogin userLogin){
|
||||
if(loginAndRegisterMapper.register(userLogin) >= 1){
|
||||
requestResult.status = RequestResult.OK;
|
||||
requestResult.message = RequestResult.USER_REGISTER_SUCCESS;
|
||||
}else {
|
||||
requestResult.status = RequestResult.FAIL;
|
||||
requestResult.message = RequestResult.USER_REGISTER_FAILED;
|
||||
}
|
||||
|
||||
return requestResult;
|
||||
}
|
||||
|
||||
public RequestResult login(UserLogin userLogin){
|
||||
UserLogin result = loginAndRegisterMapper.find(userLogin);
|
||||
RequestResult requestResult = new RequestResult();
|
||||
|
||||
if(result==null){
|
||||
requestResult.status = RequestResult.FAIL;
|
||||
requestResult.message = RequestResult.USER_LOGIN_FAILED;
|
||||
}
|
||||
else{
|
||||
String token = UUID.randomUUID().toString();//登录成功返回token
|
||||
|
||||
redisTemplate.opsForValue().set(token,"user_token",30, TimeUnit.MINUTES);//存入用户token
|
||||
|
||||
requestResult.status = RequestResult.OK;
|
||||
requestResult.message = RequestResult.USER_LOGIN_SUCCESS;
|
||||
requestResult.data = token;
|
||||
}
|
||||
|
||||
return requestResult;
|
||||
}
|
||||
|
||||
public UserLogin getPwd(UserLogin userLogin){
|
||||
return loginAndRegisterMapper.find(userLogin);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?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.xht.springboot.Dao.InformationQueryMapper">
|
||||
|
||||
<select id="queryCancerInformation" parameterType="string" resultType="com.xht.springboot.Entity.CancerInformation">
|
||||
select * from cancer_information where LOCATE(#{string},name)>=1 or LOCATE(#{string},impact)>=1
|
||||
</select>
|
||||
|
||||
</mapper>
|
Loading…
Reference in new issue