diff --git a/propertymanagement/pom.xml b/propertymanagement/pom.xml index a5892b1db5b774a4d2ef3220d822313e74600e79..a8892d25cc0668fc4fddec1f3a3f494f9281c20f 100644 --- a/propertymanagement/pom.xml +++ b/propertymanagement/pom.xml @@ -36,6 +36,12 @@ org.springframework.boot spring-boot-starter-web + + + com.alibaba + fastjson + 1.2.67 + org.mybatis.spring.boot mybatis-spring-boot-starter diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplication.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplication.java index 8750e8d5720e53b372638487c7ba9f123790b8e0..fd616e9ff813af8056bb488681f95b028a49c17f 100644 --- a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplication.java +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplication.java @@ -1,10 +1,24 @@ package com.team7.happycommunity.propertymanagement; +import com.team7.happycommunity.propertymanagement.filter.RegistorFilter; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.util.ArrayList; @SpringBootApplication +@MapperScan(value = {"com.team7.happycommunity.propertymanagement.dao"}) @EnableEurekaClient public class PropertymanagementApplication { @@ -12,4 +26,39 @@ public class PropertymanagementApplication { SpringApplication.run(PropertymanagementApplication.class, args); } + @Bean + public RedisCacheConfiguration redisCacheConfiguration(){ + RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig(); + configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); + return configuration; + } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory){ + RedisTemplate template = new RedisTemplate(); + template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + template.setKeySerializer(new StringRedisSerializer()); + template.setConnectionFactory(factory); + return template; + } + @Autowired + RegistorFilter registorFilter; + + + /** + * 配置拦截未登录请求 + * @return + */ + @Bean + public FilterRegistrationBean filterRegistrationBean(){ + FilterRegistrationBean registrationBean = new FilterRegistrationBean(); + registrationBean.setFilter(registorFilter); + ArrayList urls = new ArrayList<>(); + // 设置验证url + urls.add("/user/test"); + urls.add("/user/test2"); + registrationBean.setUrlPatterns(urls); + return registrationBean; + } + } diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/common/CommonResult.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/common/CommonResult.java new file mode 100644 index 0000000000000000000000000000000000000000..2a25e19ef7414dc02506468890023650153ed72b --- /dev/null +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/common/CommonResult.java @@ -0,0 +1,105 @@ +package com.team7.happycommunity.propertymanagement.common; + +import java.io.Serializable; + +public class CommonResult implements Serializable { + //状态码 + private Long code; + //提示消息 + private String message; + //响应数据 + private T data; + + public CommonResult(){} + public CommonResult(Long code,String message){ + this.code = code; + this.message = message; + } + + public CommonResult(Long code ,String message,T data){ + this.code = code; + this.message = message; + this.data = data; + } + + /** + * 响应成功的结果 + * @return 状态码 200 + */ + public static CommonResult success(){ + return new CommonResult(ResultUtil.SUCCESS.getCode(),ResultUtil.SUCCESS.getMessage()); + } + + public static CommonResult success(String message){ + return new CommonResult(ResultUtil.SUCCESS.getCode(),message); + } + + public static CommonResult success(T data){ + return new CommonResult(ResultUtil.SUCCESS.getCode(),ResultUtil.SUCCESS.getMessage(),data); + } + public static CommonResult success(String message,T data){ + return new CommonResult(ResultUtil.SUCCESS.getCode(),message,data); + } + + /** + * 响应失败 + * @return 状态码500 + */ + public static CommonResult failed(){ + return new CommonResult(ResultUtil.FAILED.getCode(),ResultUtil.FAILED.getMessage()); + } + + public static CommonResult failed(String message){ + return new CommonResult(ResultUtil.FAILED.getCode(),message); + } + /** + * 参数验证失败 + * @return 状态码 400 + */ + public static CommonResult valetateFailed(){ + return new CommonResult(ResultUtil.VALIDATE_FAILED.getCode(),ResultUtil.VALIDATE_FAILED.getMessage()); + } + public static CommonResult valetateFailed(String msg){ + return new CommonResult(ResultUtil.VALIDATE_FAILED.getCode(),msg); + } + + /** + * 未认证 + * @return 状态码401 + */ + public static CommonResult unathorizedFailed(){ + return new CommonResult(ResultUtil.UNAUTORIED.getCode(), ResultUtil.UNAUTORIED.getMessage()); + } + + /** + * 未授权 + * @return 状态码403 + */ + public static CommonResult forbiddenFailed(){ + return new CommonResult(ResultUtil.FORBIDDEN.getCode(),ResultUtil.FORBIDDEN.getMessage()); + } + + public Long getCode() { + return code; + } + + public void setCode(Long code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } +} diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/common/ResultUtil.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/common/ResultUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..88230127b54a5336105a79dc11a2c3d023ea4c2b --- /dev/null +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/common/ResultUtil.java @@ -0,0 +1,32 @@ +package com.team7.happycommunity.propertymanagement.common; + +public enum ResultUtil { + SUCCESS(200L,"操作成功"), + FAILED(500L,"操作失败"), + VALIDATE_FAILED(400L,"参数验证失败"), + UNAUTORIED(401L,"未认证或token过期"), + FORBIDDEN(403L,"未授权"); + private Long code; + private String message; + + private ResultUtil(Long code,String message){ + this.code = code; + this.message = message; + } + + public Long getCode() { + return code; + } + + public void setCode(Long code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} \ No newline at end of file diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/controller/UserController.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/controller/UserController.java index 5d8269a46676e284a480984b789591f127a99a9c..bec422649edfa53eb4f2320d476ade6c98673600 100644 --- a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/controller/UserController.java +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/controller/UserController.java @@ -1,9 +1,12 @@ package com.team7.happycommunity.propertymanagement.controller; +import com.team7.happycommunity.propertymanagement.common.CommonResult; +import com.team7.happycommunity.propertymanagement.pojo.PropertyManage; +import com.team7.happycommunity.propertymanagement.service.PropertyService; +import jdk.nashorn.internal.objects.annotations.Property; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -14,14 +17,14 @@ import javax.servlet.http.HttpSession; @RequestMapping("/user") public class UserController { + @Autowired + PropertyService propertyService; + @GetMapping("/test") @ResponseBody public String test(HttpServletRequest hr,String username){ HttpSession s = hr.getSession(); s.setAttribute("username",username); - System.out.println(username); -// Cookie c = new Cookie("user",username); -// hp.addCookie(c); return "测试成功!"; } @@ -34,4 +37,28 @@ public class UserController { return username == null? "未登录":username; } + + @PostMapping("/login") + @ResponseBody + public CommonResult login(HttpServletResponse httpServletResponse,@RequestBody PropertyManage propertyManage){ +// System.out.println(propertyManage.getUsername()); +// System.out.println(password); + // 1 . 登录认证 + String cookie = propertyService.login(propertyManage); + // 2 . 存入Cookie信息 + if (cookie != null){ +// System.out.println(cookie); +// Cookie cookie1 = new Cookie("user",cookie); +// cookie1.setDomain("127.0.0.1"); +// cookie1.setPath("/"); +// cookie1.setMaxAge(24*60*60); +//// httpServletResponse.addCookie(); +// httpServletResponse.addCookie(cookie1); +//// HttpSession httpSession = httpServletRequest.getSession(); +//// httpSession.setAttribute("user",cookie); + return CommonResult.success(cookie); + }else{ + return CommonResult.failed(); + } + } } diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/dao/PropertyManageMapper.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/dao/PropertyManageMapper.java index 1839e1193711a93106377002ad6002be8fbf0f19..6bb7e6b83eee82b492fc6ec0542ccfea77fc30ca 100644 --- a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/dao/PropertyManageMapper.java +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/dao/PropertyManageMapper.java @@ -1,6 +1,7 @@ package com.team7.happycommunity.propertymanagement.dao; import com.team7.happycommunity.propertymanagement.pojo.PropertyManage; +import org.apache.ibatis.annotations.Select; public interface PropertyManageMapper { int deleteByPrimaryKey(Integer id); @@ -14,4 +15,7 @@ public interface PropertyManageMapper { int updateByPrimaryKeySelective(PropertyManage record); int updateByPrimaryKey(PropertyManage record); + + @Select("select * from property_manage where username = #{username}") + PropertyManage selectByUserName(String username); } \ No newline at end of file diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/filter/RegistorFilter.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/filter/RegistorFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..1745278e67be0b595dbc37e4ec5da3ee2c6872df --- /dev/null +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/filter/RegistorFilter.java @@ -0,0 +1,63 @@ +package com.team7.happycommunity.propertymanagement.filter; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.team7.happycommunity.propertymanagement.common.CommonResult; +import com.team7.happycommunity.propertymanagement.pojo.PropertyManage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.io.PrintWriter; + +@Component +//@WebFilter(urlPatterns = "/user/test2",filterName = "authFilter") +public class RegistorFilter implements Filter { + + @Value("${spring.application.name}") + String applicationName; + + @Autowired + RedisTemplate redisTemplate; + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + + System.out.println("====filter===="); + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; + HttpSession httpSession = httpServletRequest.getSession(); + Cookie[] cookies = ((HttpServletRequest) servletRequest).getCookies(); + if(cookies == null){ + CommonResult commonResult = CommonResult.failed(); + servletResponse.setCharacterEncoding("UTF-8"); + servletResponse.setContentType("application/json; charset=utf-8"); + PrintWriter out = servletResponse.getWriter(); + out.append(JSONObject.toJSONString(commonResult)); + }else{ + String cookie_get = null; + for(Cookie cookie :cookies){ + if (cookie.getName().equals("user")){ + cookie_get = cookie.getValue(); + } + } + HashOperations hashOperations = redisTemplate.opsForHash(); + PropertyManage propertyManage = (PropertyManage)hashOperations.get("hash:"+applicationName+":user",cookie_get); + System.out.println(propertyManage.toString()); + filterChain.doFilter(servletRequest,servletResponse); + System.out.println("====filter===="); + } +// PropertyManage propertyManage = (PropertyManage)hashOperations.get("hash:"+applicationName+":user",cookie); +// System.out.println(propertyManage.toString()); +// filterChain.doFilter(servletRequest,servletResponse); +// System.out.println("====filter===="); + + } +} diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/AreaInfo.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/AreaInfo.java index 9066fe6c5aa19d8150093b129a2a9000f98a8b0f..c0615067b00d86630074b32261cc881a66a0fc35 100644 --- a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/AreaInfo.java +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/AreaInfo.java @@ -1,6 +1,11 @@ package com.team7.happycommunity.propertymanagement.pojo; -public class AreaInfo { +import lombok.Data; + +import java.io.Serializable; + +@Data +public class AreaInfo implements Serializable { private Integer id; private String areaName; diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/PropertyManage.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/PropertyManage.java index aa425d69a0a24030259f99fdfe1f88b14cd569c3..b3348ab0b0c4b85d68d167803ad21f534380e0aa 100644 --- a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/PropertyManage.java +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/pojo/PropertyManage.java @@ -1,8 +1,12 @@ package com.team7.happycommunity.propertymanagement.pojo; +import lombok.Data; + +import java.io.Serializable; import java.util.Date; -public class PropertyManage { +@Data +public class PropertyManage implements Serializable { private Integer id; private Integer villageId; @@ -21,6 +25,10 @@ public class PropertyManage { private Date passTime; + public PropertyManage(){ + + } + public Integer getId() { return id; } diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/service/PropertyService.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/service/PropertyService.java new file mode 100644 index 0000000000000000000000000000000000000000..ab2cdbd39ef3d82f72252eb7dbfbdcd75dad7753 --- /dev/null +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/service/PropertyService.java @@ -0,0 +1,7 @@ +package com.team7.happycommunity.propertymanagement.service; + +import com.team7.happycommunity.propertymanagement.pojo.PropertyManage; + +public interface PropertyService { + String login(PropertyManage propertyManage); +} diff --git a/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/service/impl/PropertyServiceImpl.java b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/service/impl/PropertyServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..0d8e02ebdd7ff4da6bfaf7243540400bb1741c55 --- /dev/null +++ b/propertymanagement/src/main/java/com/team7/happycommunity/propertymanagement/service/impl/PropertyServiceImpl.java @@ -0,0 +1,52 @@ +package com.team7.happycommunity.propertymanagement.service.impl; + +import com.team7.happycommunity.propertymanagement.dao.PropertyManageMapper; +import com.team7.happycommunity.propertymanagement.pojo.PropertyManage; +import com.team7.happycommunity.propertymanagement.service.PropertyService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.util.DigestUtils; + +import java.util.UUID; + +@Service +public class PropertyServiceImpl implements PropertyService { + + @Autowired + PropertyManageMapper propertyManageMapper; + + @Autowired + RedisTemplate redisTemplate; + + @Value("${spring.application.name}") + String applicationName; + + /** + * 登录业务 加盐认证 生成Cookie信息 存入redisTemplate内 + * @param propertyManage + * @return + */ + @Override + public String login(PropertyManage propertyManage) { + PropertyManage propertyManage_re = propertyManageMapper.selectByUserName(propertyManage.getUsername()); + // 生成加密后字段 + String result_pass = DigestUtils.md5DigestAsHex((propertyManage.getPassword()+propertyManage_re.getSaltValue()).getBytes()); + if(result_pass.equals(propertyManage_re.getPassword())){ + // 认证成功 RedisTemplate存入用户信息 + propertyManage_re.setPassword(null); + propertyManage_re.setSaltValue(null); + HashOperations hashOperations = redisTemplate.opsForHash(); + // 设置用户信息 cookie 值并作为redis key值 + String uuid = UUID.randomUUID().toString().replace("-",""); + hashOperations.put("hash:"+applicationName+":user",uuid, propertyManage_re); + return uuid; + }else{ + // 认证失败 返回空 + return null; + } +// return null; + } +} diff --git a/propertymanagement/src/main/resources/application.yml b/propertymanagement/src/main/resources/application.yml index cb979ed5331f6f007844e9770652d359bd1f0f6a..cdefe9bcd1cf7a008a21abebad648edfe437ff06 100644 --- a/propertymanagement/src/main/resources/application.yml +++ b/propertymanagement/src/main/resources/application.yml @@ -1,4 +1,14 @@ spring: + redis: + password: hc123hc + jedis: + pool: + max-idle: 10 + max-wait: 1000ms + max-active: 50 + sentinel: + master: mysentinel1 + nodes: 106.12.148.100:26380,106.12.148.100:26381,106.12.148.100:26382 application: name: propertymanagement thymeleaf: diff --git a/propertymanagement/src/test/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplicationTests.java b/propertymanagement/src/test/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplicationTests.java index 42e2bc9bbc97e73a6035ff90184a20ac942ec299..a0516b3b2ce7e218a61f855a428cae87a8033d9d 100644 --- a/propertymanagement/src/test/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplicationTests.java +++ b/propertymanagement/src/test/java/com/team7/happycommunity/propertymanagement/PropertymanagementApplicationTests.java @@ -1,13 +1,87 @@ package com.team7.happycommunity.propertymanagement; +import com.team7.happycommunity.propertymanagement.dao.AreaInfoMapper; +import com.team7.happycommunity.propertymanagement.dao.PropertyManageMapper; +import com.team7.happycommunity.propertymanagement.pojo.AreaInfo; +import com.team7.happycommunity.propertymanagement.pojo.PropertyManage; import org.junit.jupiter.api.Test; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.util.DigestUtils; + +import java.util.Date; +import java.util.UUID; @SpringBootTest +@MapperScan(value = {"com.team7.happycommunity.propertymanagement.dao"}) class PropertymanagementApplicationTests { + @Autowired + AreaInfoMapper areaInfoMapper; + + @Autowired + PropertyManageMapper propertyManageMapper; + + /** + * 脚本写入小区信息 + */ + public void create_areas(){ + AreaInfo areaInfo = new AreaInfo(); + areaInfo.setDetail1("111"); + areaInfo.setAreaAddress("四川省成都市金牛区天府三街666号"); + areaInfo.setAreaName("宜家小区"); + areaInfoMapper.insertSelective(areaInfo); + } + + /** + * 脚本写入物业信息 + */ + public void create_property(){ + // PropertyManage(id=null, villageId=null, + // username=root, + // password=8b575703b834b314a8498107710cf274, + // saltValue=758f270847ef40358f787a9761525b3e, + // phone=1551669999, + // status=1, + // createTime=Tue Mar 24 09:21:26 CST 2020, + // passTime=null) + PropertyManage propertyManage = new PropertyManage(); + AreaInfo areaInfo = areaInfoMapper.selectByPrimaryKey(2); + String password = "mypass"; + String uuid = UUID.randomUUID().toString().replace("-",""); + propertyManage.setUsername("admin"); + propertyManage.setPassword(""); + propertyManage.setCreateTime(new Date()); + propertyManage.setStatus(1); + propertyManage.setPhone("1551669999"); + propertyManage.setSaltValue(uuid); + String pass = DigestUtils.md5DigestAsHex((password+uuid).getBytes()); + propertyManage.setPassword(pass); + propertyManage.setVillageId(areaInfo.getId()); +// System.out.println(propertyManage.toString()); + propertyManageMapper.insertSelective(propertyManage); + } + + @Autowired + RedisTemplate redisTemplate; + + /** + * 测试sentinel哨兵是否可用 + */ + public void testRedisSentinel(){ + HashOperations hashOperations = redisTemplate.opsForHash(); + hashOperations.put("hash:mykey","hc",propertyManageMapper.selectByPrimaryKey(1)); + PropertyManage propertyManage = (PropertyManage) hashOperations.get("hash:mykey","hc"); + System.out.println(propertyManage.toString()); + } + @Test void contextLoads() { + String salt = "b7a079dc115042f99d9e215baa03204f"; + } }