2021-04-21 已登录用户,修改自己密码,需要验证原密码和新密码

This commit is contained in:
2021-04-21 15:43:20 +08:00
parent 925fe8aa83
commit 6be908dc65
6 changed files with 204 additions and 156 deletions

View File

@@ -22,7 +22,7 @@ import tk.mybatis.spring.annotation.MapperScan;
@EnableCaching
@MapperScan(basePackages = "com.hcframe.**.dao")
@ComponentScan(basePackages = {"com.hcframe.**"},excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {DataSourceConfiguration.class}))
@EnableDiscoveryClient
//@EnableDiscoveryClient
@Import(CasClientConfigurationProperties.class)
//@EnableCasClient
public class UserApplication {

View File

@@ -1,82 +1,82 @@
package com.hcframe.user.common.config;
import com.hcframe.base.module.auth.dao.FtUserDao;
import com.hcframe.base.module.auth.entity.FtUser;
import com.hcframe.base.module.shiro.service.ShiroType;
import com.hcframe.base.module.shiro.service.SystemRealm;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
/**
* @author lhc
* @date 2021-02-05
* @decription shiro 配置类
*/
@Component
public class ShiroRealmConfig implements SystemRealm {
final
FtUserDao ftUserDao;
public ShiroRealmConfig(FtUserDao ftUserDao) {
this.ftUserDao = ftUserDao;
}
/**
* 根据用户信息注入权限
* @param user 用户信息
* @return 权限信息
*/
@Override
public SimpleAuthorizationInfo setAuthoriztion(Object user) {
return new SimpleAuthorizationInfo();
}
/**
* 根据用户Id查询用户信息并注入到shiro框架中
* @param userId 用户id
* @return 用户信息
*/
@Override
public Object findByUserId(String userId) {
return ftUserDao.selectOne(FtUser.builder().userId(Integer.parseInt(userId)).build());
}
/**
* 配置拦截及放行路径
* @return 返回拦截及放行路径Map
*/
@Override
public LinkedHashMap<String, String> setShiroUrl() {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
// 用户登陆
map.put("/ftUser/login", ShiroType.ANON);
// Vue静态资源
map.put("/img/**", ShiroType.ANON);
map.put("/static/**", ShiroType.ANON);
map.put("/tinymce/**", ShiroType.ANON);
map.put("/favicon.ico", ShiroType.ANON);
map.put("/manifest.json", ShiroType.ANON);
map.put("/robots.txt", ShiroType.ANON);
map.put("/precache*", ShiroType.ANON);
map.put("/service-worker.js", ShiroType.ANON);
// swagger UI 静态资源
map.put("/swagger-ui.html",ShiroType.ANON);
map.put("/doc.html",ShiroType.ANON);
map.put("/swagger-resources/**",ShiroType.ANON);
map.put("/webjars/**",ShiroType.ANON);
map.put("/v2/api-docs",ShiroType.ANON);
map.put("/v2/api-docs-ext",ShiroType.ANON);
map.put("/swagger/**",ShiroType.ANON);
// druid 资源路径
map.put("/druid/**",ShiroType.ANON);
map.put("/cas/valid",ShiroType.ANON);
map.put("/cas/logout",ShiroType.ANON);
// 其余路径均拦截
map.put("/**", ShiroType.AUTH);
return map;
}
}
package com.hcframe.user.common.config;
import com.hcframe.base.module.auth.dao.FtUserDao;
import com.hcframe.base.module.auth.entity.FtUser;
import com.hcframe.base.module.shiro.service.ShiroType;
import com.hcframe.base.module.shiro.service.SystemRealm;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
/**
* @author lhc
* @date 2021-02-05
* @decription shiro 配置类
*/
@Component
public class ShiroRealmConfig implements SystemRealm {
final
FtUserDao ftUserDao;
public ShiroRealmConfig(FtUserDao ftUserDao) {
this.ftUserDao = ftUserDao;
}
/**
* 根据用户信息注入权限
* @param user 用户信息
* @return 权限信息
*/
@Override
public SimpleAuthorizationInfo setAuthoriztion(Object user) {
return new SimpleAuthorizationInfo();
}
/**
* 根据用户Id查询用户信息并注入到shiro框架中
* @param userId 用户id
* @return 用户信息
*/
@Override
public Object findByUserId(String userId) {
return ftUserDao.selectOne(FtUser.builder().userId(Integer.parseInt(userId)).build());
}
/**
* 配置拦截及放行路径
* @return 返回拦截及放行路径Map
*/
@Override
public LinkedHashMap<String, String> setShiroUrl() {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
// 用户登陆
map.put("/ftUser/login", ShiroType.ANON);
// Vue静态资源
map.put("/img/**", ShiroType.ANON);
map.put("/static/**", ShiroType.ANON);
map.put("/tinymce/**", ShiroType.ANON);
map.put("/favicon.ico", ShiroType.ANON);
map.put("/manifest.json", ShiroType.ANON);
map.put("/robots.txt", ShiroType.ANON);
map.put("/precache*", ShiroType.ANON);
map.put("/service-worker.js", ShiroType.ANON);
// swagger UI 静态资源
map.put("/swagger-ui.html",ShiroType.ANON);
map.put("/doc.html",ShiroType.ANON);
map.put("/swagger-resources/**",ShiroType.ANON);
map.put("/webjars/**",ShiroType.ANON);
map.put("/v2/api-docs",ShiroType.ANON);
map.put("/v2/api-docs-ext",ShiroType.ANON);
map.put("/swagger/**",ShiroType.ANON);
// druid 资源路径
map.put("/druid/**",ShiroType.ANON);
map.put("/cas/valid",ShiroType.ANON);
map.put("/cas/logout",ShiroType.ANON);
// 其余路径均拦截
map.put("/**", ShiroType.AUTH);
return map;
}
}

View File

@@ -1,73 +1,73 @@
package com.hcframe.user.module.manage.controller;
import com.hcframe.base.common.ResultVO;
import com.hcframe.redis.RedisUtil;
import net.unicon.cas.client.configuration.CasClientConfigurationProperties;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
@Controller
@RequestMapping("cas")
public class CasController {
final
RedisUtil redisUtil;
final
CasClientConfigurationProperties casClientConfigurationProperties;
public CasController(RedisUtil redisUtil, CasClientConfigurationProperties casClientConfigurationProperties) {
this.redisUtil = redisUtil;
this.casClientConfigurationProperties = casClientConfigurationProperties;
}
@GetMapping("valid")
public ResultVO<String> casValid(HttpServletResponse response, HttpServletRequest request,String webUrl) {
String token = "";
// token = request.getHeader("X-Access-Token");
try {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if ("X-Access-Token".equals(cookie.getName())) {
token = cookie.getValue();
response.addCookie(cookie);
break;
}
}
webUrl = URLDecoder.decode(webUrl, "utf-8");
response.sendRedirect("http://"+webUrl+"/#/?token=" + token );
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@GetMapping("userinfo")
@ResponseBody
public ResultVO<Object> getUserInfo(String token) {
// return ResultVO.getSuccess(redisUtil.hget("session", token));
return ResultVO.getSuccess(SecurityUtils.getSubject().getPrincipal());
}
@GetMapping("/logout")
@ResponseBody
public ResultVO<String> logout(HttpServletRequest request, @RequestHeader("X-Access-Token") String token) {
Cookie cookie = new Cookie("X-Access-Token", null);
cookie.setMaxAge(0);
String headerToken = request.getHeader("X-Access-Token");
redisUtil.hdel("session", token);
redisUtil.hdel("session", headerToken);
Subject subject = SecurityUtils.getSubject();
subject.logout();
return ResultVO.getSuccess(casClientConfigurationProperties.getServerUrlPrefix()+"/logout");
}
}
package com.hcframe.user.module.manage.controller;
import com.hcframe.base.common.ResultVO;
import com.hcframe.redis.RedisUtil;
import net.unicon.cas.client.configuration.CasClientConfigurationProperties;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
@Controller
@RequestMapping("cas")
public class CasController {
final
RedisUtil redisUtil;
final
CasClientConfigurationProperties casClientConfigurationProperties;
public CasController(RedisUtil redisUtil, CasClientConfigurationProperties casClientConfigurationProperties) {
this.redisUtil = redisUtil;
this.casClientConfigurationProperties = casClientConfigurationProperties;
}
@GetMapping("valid")
public ResultVO<String> casValid(HttpServletResponse response, HttpServletRequest request,String webUrl) {
String token = "";
// token = request.getHeader("X-Access-Token");
try {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if ("X-Access-Token".equals(cookie.getName())) {
token = cookie.getValue();
response.addCookie(cookie);
break;
}
}
webUrl = URLDecoder.decode(webUrl, "utf-8");
response.sendRedirect("http://"+webUrl+"/#/?token=" + token );
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@GetMapping("userinfo")
@ResponseBody
public ResultVO<Object> getUserInfo(String token) {
// return ResultVO.getSuccess(redisUtil.hget("session", token));
return ResultVO.getSuccess(SecurityUtils.getSubject().getPrincipal());
}
@GetMapping("/logout")
@ResponseBody
public ResultVO<String> logout(HttpServletRequest request, @RequestHeader("X-Access-Token") String token) {
Cookie cookie = new Cookie("X-Access-Token", null);
cookie.setMaxAge(0);
String headerToken = request.getHeader("X-Access-Token");
redisUtil.hdel("session", token);
redisUtil.hdel("session", headerToken);
Subject subject = SecurityUtils.getSubject();
subject.logout();
return ResultVO.getSuccess(casClientConfigurationProperties.getServerUrlPrefix()+"/logout");
}
}

View File

@@ -66,6 +66,12 @@ public class ManageController {
public ResultVO<Integer> resetPassword(String userId,@PathVariable Integer version) {
return manageService.resetPassword(userId,version);
}
@PutMapping("changePassword")
@ApiOperation(value = "修改密码",notes = "用户输入原密码和新密码")
public ResultVO<Integer> changePassword(String pwd,String npwd,String npwd2) {
return manageService.changePassword(pwd,npwd,npwd2);
}
@GetMapping("/sync")
public ResultVO<Object> sync() {

View File

@@ -20,4 +20,6 @@ public interface ManageService {
ResultVO<Integer> disable(Boolean enabled, String userId, Integer version);
ResultVO<Object> sync();
ResultVO<Integer> changePassword(String pwd, String npwd, String npwd2);
}

View File

@@ -8,6 +8,7 @@ import com.hcframe.base.common.ServiceException;
import com.hcframe.base.common.WebPageInfo;
import com.hcframe.base.common.utils.DateUtil;
import com.hcframe.base.common.utils.JudgeException;
import com.hcframe.base.module.data.constants.FieldConstants;
import com.hcframe.base.module.data.module.BaseMapper;
import com.hcframe.base.module.data.module.BaseMapperImpl;
import com.hcframe.base.module.data.module.Condition;
@@ -22,6 +23,7 @@ import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -196,4 +198,42 @@ public class ManageServiceDataImpl implements ManageService {
}
return null;
}
@Override
public ResultVO<Integer> changePassword(String pwd, String npwd, String npwd2) {
JudgeException.isNull(pwd,"密码不能为空");
JudgeException.isNull(npwd,"新密码不能为空");
if(!npwd.equals(npwd2)) {
return ResultVO.getFailed("两次新密码输入不一致");
}
Map<String, Object> user = (Map<String, Object>) SecurityUtils.getSubject().getPrincipal();
String id = (String) user.get("ID");
Map<String, Object> data = baseMapper.selectByPk(TABLE_NAME,PK_ID,id);
Integer version = Integer.parseInt(data.get(FieldConstants.VERSION.toString()).toString());
try {
if(!data.get("PASSWORD").equals(MD5Utils.encode(pwd))) {
return ResultVO.getFailed("原密码错误");
}
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
logger.error("验证密码失败",e);
throw new ServiceException(e);
}
Map<String, Object> map = new HashMap<>(2);
map.put(PK_ID, id);
try {
map.put("PASSWORD",MD5Utils.encode(npwd));
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
logger.error("重置密码失败",e);
throw new ServiceException(e);
}
return tableService.updateWithDate(TABLE_INFO,map,version);
}
}