正在显示
9 个修改的文件
包含
149 行增加
和
94 行删除
| @@ -4,13 +4,18 @@ | @@ -4,13 +4,18 @@ | ||
| 4 | */ | 4 | */ |
| 5 | package com.tianbo.warehouse; | 5 | package com.tianbo.warehouse; |
| 6 | 6 | ||
| 7 | +import com.google.code.kaptcha.impl.DefaultKaptcha; | ||
| 8 | +import com.google.code.kaptcha.util.Config; | ||
| 7 | import org.mybatis.spring.annotation.MapperScan; | 9 | import org.mybatis.spring.annotation.MapperScan; |
| 8 | import org.springframework.boot.SpringApplication; | 10 | import org.springframework.boot.SpringApplication; |
| 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; | 11 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 10 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; | 12 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; |
| 13 | +import org.springframework.context.annotation.Bean; | ||
| 11 | import org.springframework.scheduling.annotation.EnableScheduling; | 14 | import org.springframework.scheduling.annotation.EnableScheduling; |
| 12 | import org.springframework.transaction.annotation.EnableTransactionManagement; | 15 | import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 13 | 16 | ||
| 17 | +import java.util.Properties; | ||
| 18 | + | ||
| 14 | @SpringBootApplication | 19 | @SpringBootApplication |
| 15 | @EnableScheduling | 20 | @EnableScheduling |
| 16 | @EnableEurekaClient | 21 | @EnableEurekaClient |
| @@ -22,5 +27,24 @@ public class WarehouseApplication { | @@ -22,5 +27,24 @@ public class WarehouseApplication { | ||
| 22 | SpringApplication.run(WarehouseApplication.class, args); | 27 | SpringApplication.run(WarehouseApplication.class, args); |
| 23 | } | 28 | } |
| 24 | 29 | ||
| 30 | + /*声明验证码生成策略属性 Bean*/ | ||
| 31 | + @Bean | ||
| 32 | + public DefaultKaptcha captchaProducer(){ | ||
| 33 | + DefaultKaptcha captchaProducer =new DefaultKaptcha(); | ||
| 34 | + Properties properties =new Properties(); | ||
| 35 | + properties.setProperty("kaptcha.border","yes"); | ||
| 36 | + properties.setProperty("kaptcha.border.color","105,179,90"); | ||
| 37 | + properties.setProperty("kaptcha.textproducer.font.color","red"); | ||
| 38 | + properties.setProperty("kaptcha.image.width","125"); | ||
| 39 | + properties.setProperty("kaptcha.image.height","60"); | ||
| 40 | + properties.setProperty("kaptcha.textproducer.font.size","36"); | ||
| 41 | + properties.setProperty("kaptcha.session.key","code"); | ||
| 42 | + properties.setProperty("kaptcha.textproducer.char.length","4"); | ||
| 43 | + properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑"); | ||
| 44 | + Config config=new Config(properties); | ||
| 45 | + captchaProducer.setConfig(config); | ||
| 46 | + return captchaProducer; | ||
| 47 | + } | ||
| 48 | + | ||
| 25 | } | 49 | } |
| 26 | 50 |
| 1 | +package com.tianbo.warehouse.controller; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.JSON; | ||
| 4 | + | ||
| 5 | +import com.google.code.kaptcha.impl.DefaultKaptcha; | ||
| 6 | + | ||
| 7 | +import com.thoughtworks.xstream.core.util.Base64Encoder; | ||
| 8 | +import com.tianbo.warehouse.controller.response.ResultJson; | ||
| 9 | +import com.tianbo.warehouse.model.ROLE; | ||
| 10 | +import com.tianbo.warehouse.service.RoleService; | ||
| 11 | + | ||
| 12 | +import com.tianbo.warehouse.util.RedisUtils; | ||
| 13 | + | ||
| 14 | +import lombok.extern.slf4j.Slf4j; | ||
| 15 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 16 | +import org.springframework.web.bind.annotation.PostMapping; | ||
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 18 | +import org.springframework.web.bind.annotation.RestController; | ||
| 19 | + | ||
| 20 | +import javax.imageio.ImageIO; | ||
| 21 | + | ||
| 22 | +import javax.servlet.http.Cookie; | ||
| 23 | +import javax.servlet.http.HttpServletRequest; | ||
| 24 | +import javax.servlet.http.HttpServletResponse; | ||
| 25 | +import javax.servlet.http.HttpSession; | ||
| 26 | +import java.awt.image.BufferedImage; | ||
| 27 | +import java.io.ByteArrayOutputStream; | ||
| 28 | +import java.io.IOException; | ||
| 29 | +import java.util.HashMap; | ||
| 30 | +import java.util.List; | ||
| 31 | +import java.util.Map; | ||
| 32 | +import java.util.UUID; | ||
| 33 | + | ||
| 34 | +@Slf4j | ||
| 35 | +@RestController() | ||
| 36 | +@RequestMapping("/anonymous") | ||
| 37 | +public class AnonymousController { | ||
| 38 | + | ||
| 39 | + @Autowired | ||
| 40 | + RoleService roleService; | ||
| 41 | + | ||
| 42 | + @Autowired | ||
| 43 | + RedisUtils redisUtils; | ||
| 44 | + | ||
| 45 | + @Autowired | ||
| 46 | + private DefaultKaptcha captchaProducer; | ||
| 47 | + | ||
| 48 | + /** | ||
| 49 | + * 配置匿名者可以访问的路由,并更新到redis,匿名者默认可以访问的role_name =ROLE_anonymous | ||
| 50 | + * 此方法会将所有符合权限组名=ROLE_anonymous的权限更新到redis中,供gateway调用判断权限 | ||
| 51 | + * @return | ||
| 52 | + */ | ||
| 53 | + @PostMapping("initAnonymousRoute") | ||
| 54 | + public ResultJson initAnonymousRoute(){ | ||
| 55 | + List<ROLE> list = roleService.getROLE_anonymousPermList(); | ||
| 56 | + String json = JSON.toJSONString(list); | ||
| 57 | + boolean result= redisUtils.set("ROLE_anonymous_routers", json,0); | ||
| 58 | + return result ? new ResultJson("200","匿名者权限配置成功") :new ResultJson("500","匿名者权限配置失败"); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + /** | ||
| 62 | + * 生成验证码 | ||
| 63 | + */ | ||
| 64 | + @RequestMapping(value = "/randCode") | ||
| 65 | + public ResultJson getRandCode(){ | ||
| 66 | + | ||
| 67 | + // 获取验证码上的文字 | ||
| 68 | + String capText = captchaProducer.createText(); | ||
| 69 | + | ||
| 70 | + // 将文件渲染到图片上 | ||
| 71 | + BufferedImage bi = captchaProducer.createImage(capText); | ||
| 72 | + ByteArrayOutputStream outputStream = null; | ||
| 73 | + outputStream = new ByteArrayOutputStream(); | ||
| 74 | + Base64Encoder encoder = new Base64Encoder(); | ||
| 75 | + Map<String,Object> map = new HashMap<>(); | ||
| 76 | + String verifyToken = ""; | ||
| 77 | + try { | ||
| 78 | + verifyToken = UUID.randomUUID().toString(); | ||
| 79 | + redisUtils.set("verifyToken_" + verifyToken,capText,120); | ||
| 80 | + ImageIO.write(bi, "jpeg", outputStream); | ||
| 81 | + map.put("verifyImg","data:image/jpeg;base64,"+encoder.encode(outputStream.toByteArray())); | ||
| 82 | + } catch (IOException e) { | ||
| 83 | + e.printStackTrace(); | ||
| 84 | + return new ResultJson("500","verify get error"); | ||
| 85 | + } | ||
| 86 | + return new ResultJson("200","verify get ok",map,verifyToken); | ||
| 87 | + | ||
| 88 | + } | ||
| 89 | +} |
| 1 | package com.tianbo.warehouse.controller; | 1 | package com.tianbo.warehouse.controller; |
| 2 | 2 | ||
| 3 | +import com.tianbo.warehouse.controller.response.ResultJson; | ||
| 3 | import org.springframework.web.bind.annotation.GetMapping; | 4 | import org.springframework.web.bind.annotation.GetMapping; |
| 5 | +import org.springframework.web.bind.annotation.PostMapping; | ||
| 4 | import org.springframework.web.bind.annotation.RequestMapping; | 6 | import org.springframework.web.bind.annotation.RequestMapping; |
| 5 | import org.springframework.web.bind.annotation.RestController; | 7 | import org.springframework.web.bind.annotation.RestController; |
| 6 | 8 | ||
| @@ -16,5 +18,4 @@ public class IndexController { | @@ -16,5 +18,4 @@ public class IndexController { | ||
| 16 | } | 18 | } |
| 17 | 19 | ||
| 18 | 20 | ||
| 19 | - | ||
| 20 | } | 21 | } |
| @@ -27,15 +27,6 @@ import java.util.Properties; | @@ -27,15 +27,6 @@ import java.util.Properties; | ||
| 27 | @RestController | 27 | @RestController |
| 28 | public class MainController { | 28 | public class MainController { |
| 29 | 29 | ||
| 30 | - @Autowired | ||
| 31 | - private DefaultKaptcha captchaProducer; | ||
| 32 | - | ||
| 33 | - @Autowired | ||
| 34 | - private UserService userService; | ||
| 35 | - | ||
| 36 | - @Autowired | ||
| 37 | - private RedisUtils redisUtils; | ||
| 38 | - | ||
| 39 | @GetMapping("/error") | 30 | @GetMapping("/error") |
| 40 | public String error(){ | 31 | public String error(){ |
| 41 | return "error"; | 32 | return "error"; |
| @@ -45,75 +36,4 @@ public class MainController { | @@ -45,75 +36,4 @@ public class MainController { | ||
| 45 | public String main(){ | 36 | public String main(){ |
| 46 | return "main"; | 37 | return "main"; |
| 47 | } | 38 | } |
| 48 | - | ||
| 49 | - /** | ||
| 50 | - * 生成验证码 | ||
| 51 | - */ | ||
| 52 | - @RequestMapping(value = "/randCode") | ||
| 53 | - public void getRandCode(HttpServletRequest request, HttpServletResponse response){ | ||
| 54 | - | ||
| 55 | - HttpSession session = request.getSession(); | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - response.setDateHeader("Expires", 0); | ||
| 59 | - | ||
| 60 | - // Set standard HTTP/1.1 no-cache headers. | ||
| 61 | - response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); | ||
| 62 | - | ||
| 63 | - // Set IE extended HTTP/1.1 no-cache headers (use addHeader). | ||
| 64 | - response.addHeader("Cache-Control", "post-check=0, pre-check=0"); | ||
| 65 | - | ||
| 66 | - // Set standard HTTP/1.0 no-cache header. | ||
| 67 | - response.setHeader("Pragma", "no-cache"); | ||
| 68 | - | ||
| 69 | - // 设置返回文件类型 | ||
| 70 | - response.setContentType("image/jpeg"); | ||
| 71 | - | ||
| 72 | - // 获取验证码上的文字 | ||
| 73 | - String capText = captchaProducer.createText(); | ||
| 74 | - | ||
| 75 | - // 将验证码上的文字保存在session中 | ||
| 76 | - session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); | ||
| 77 | - | ||
| 78 | - //缓存存储登录验证码信息 | ||
| 79 | - redisUtils.set(session.getId(),capText,60); | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY); | ||
| 83 | - | ||
| 84 | - log.info("验证码为:"+code); | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - // 将文件渲染到图片上 | ||
| 88 | - BufferedImage bi = captchaProducer.createImage(capText); | ||
| 89 | - ServletOutputStream out = null; | ||
| 90 | - try { | ||
| 91 | - out = response.getOutputStream(); | ||
| 92 | - ImageIO.write(bi, "jpeg", out); | ||
| 93 | - out.flush(); | ||
| 94 | - } catch (IOException e) { | ||
| 95 | - e.printStackTrace(); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - } | ||
| 100 | - | ||
| 101 | - /*声明验证码生成策略属性 Bean*/ | ||
| 102 | - @Bean | ||
| 103 | - public DefaultKaptcha captchaProducer(){ | ||
| 104 | - DefaultKaptcha captchaProducer =new DefaultKaptcha(); | ||
| 105 | - Properties properties =new Properties(); | ||
| 106 | - properties.setProperty("kaptcha.border","yes"); | ||
| 107 | - properties.setProperty("kaptcha.border.color","105,179,90"); | ||
| 108 | - properties.setProperty("kaptcha.textproducer.font.color","red"); | ||
| 109 | - properties.setProperty("kaptcha.image.width","125"); | ||
| 110 | - properties.setProperty("kaptcha.image.height","60"); | ||
| 111 | - properties.setProperty("kaptcha.textproducer.font.size","36"); | ||
| 112 | - properties.setProperty("kaptcha.session.key","code"); | ||
| 113 | - properties.setProperty("kaptcha.textproducer.char.length","4"); | ||
| 114 | - properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑"); | ||
| 115 | - Config config=new Config(properties); | ||
| 116 | - captchaProducer.setConfig(config); | ||
| 117 | - return captchaProducer; | ||
| 118 | - } | ||
| 119 | } | 39 | } |
| @@ -40,4 +40,11 @@ public class ResultJson<T> implements Serializable{ | @@ -40,4 +40,11 @@ public class ResultJson<T> implements Serializable{ | ||
| 40 | this.msg = msg; | 40 | this.msg = msg; |
| 41 | this.data = data; | 41 | this.data = data; |
| 42 | } | 42 | } |
| 43 | + | ||
| 44 | + public ResultJson(String code, String msg, T data,String jwtToken) { | ||
| 45 | + this.code = code; | ||
| 46 | + this.msg = msg; | ||
| 47 | + this.data = data; | ||
| 48 | + this.jwtToken = jwtToken; | ||
| 49 | + } | ||
| 43 | } | 50 | } |
src/main/java/com/tianbo/warehouse/security/handel/kakologin/MyLoginAuthenticationProcessFilter.java
| @@ -16,6 +16,7 @@ import org.springframework.security.web.authentication.preauth.PreAuthenticatedC | @@ -16,6 +16,7 @@ import org.springframework.security.web.authentication.preauth.PreAuthenticatedC | ||
| 16 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | 16 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 17 | import org.springframework.stereotype.Component; | 17 | import org.springframework.stereotype.Component; |
| 18 | 18 | ||
| 19 | +import javax.servlet.http.Cookie; | ||
| 19 | import javax.servlet.http.HttpServletRequest; | 20 | import javax.servlet.http.HttpServletRequest; |
| 20 | import javax.servlet.http.HttpServletResponse; | 21 | import javax.servlet.http.HttpServletResponse; |
| 21 | import javax.servlet.http.HttpSession; | 22 | import javax.servlet.http.HttpSession; |
| @@ -53,21 +54,19 @@ public class MyLoginAuthenticationProcessFilter extends AbstractAuthenticationPr | @@ -53,21 +54,19 @@ public class MyLoginAuthenticationProcessFilter extends AbstractAuthenticationPr | ||
| 53 | String loginUserName = request.getParameter("username"); | 54 | String loginUserName = request.getParameter("username"); |
| 54 | String loginUserPass = request.getParameter("password"); | 55 | String loginUserPass = request.getParameter("password"); |
| 55 | String loginVerify = request.getParameter("verify"); | 56 | String loginVerify = request.getParameter("verify"); |
| 57 | + String verifyToken = request.getParameter("verifyToken"); | ||
| 56 | 58 | ||
| 57 | -// //验证码判断 | ||
| 58 | -// HttpSession session = request.getSession(); | ||
| 59 | -// String verify = ""; | ||
| 60 | -//// String verify = redisUtils.get(session.getId()); | ||
| 61 | -// if (session.getAttribute(Constants.KAPTCHA_SESSION_KEY)!=null){ | ||
| 62 | -// verify = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString(); | ||
| 63 | -// } | ||
| 64 | -// | ||
| 65 | -// if(verify!= null && !verify.equals(loginVerify)){ | ||
| 66 | -// throw new BadCredentialsException("验证码错误!"); | ||
| 67 | -// } | 59 | + //验证码判断 |
| 60 | + String verify = ""; | ||
| 68 | 61 | ||
| 69 | - authRequest = new UsernamePasswordAuthenticationToken(loginUserName,loginUserPass, null); | ||
| 70 | - authRequest.setDetails(authenticationDetailsSource.buildDetails(request)); | 62 | + verify = redisUtils.get("verifyToken_" + verifyToken); |
| 63 | + | ||
| 64 | + if(verify != null && loginVerify != null && verify.equals(loginVerify)){ | ||
| 65 | + authRequest = new UsernamePasswordAuthenticationToken(loginUserName,loginUserPass, null); | ||
| 66 | + authRequest.setDetails(authenticationDetailsSource.buildDetails(request)); | ||
| 67 | + }else { | ||
| 68 | + throw new BadCredentialsException("验证码错误!"); | ||
| 69 | + } | ||
| 71 | } catch (BadCredentialsException e){ | 70 | } catch (BadCredentialsException e){ |
| 72 | throw new PreAuthenticatedCredentialsNotFoundException(e.getMessage()); | 71 | throw new PreAuthenticatedCredentialsNotFoundException(e.getMessage()); |
| 73 | }catch (Exception e) { | 72 | }catch (Exception e) { |
| @@ -4,9 +4,13 @@ import com.github.pagehelper.PageInfo; | @@ -4,9 +4,13 @@ import com.github.pagehelper.PageInfo; | ||
| 4 | import com.tianbo.warehouse.model.ROLE; | 4 | import com.tianbo.warehouse.model.ROLE; |
| 5 | import com.tianbo.warehouse.model.RolePermission; | 5 | import com.tianbo.warehouse.model.RolePermission; |
| 6 | 6 | ||
| 7 | +import java.util.List; | ||
| 8 | + | ||
| 7 | public interface RoleService { | 9 | public interface RoleService { |
| 8 | PageInfo<ROLE> findAll(int pageNum, int pageSize, String roleName, String type); | 10 | PageInfo<ROLE> findAll(int pageNum, int pageSize, String roleName, String type); |
| 9 | 11 | ||
| 12 | + List<ROLE> getROLE_anonymousPermList(); | ||
| 13 | + | ||
| 10 | int insertSelective(ROLE record); | 14 | int insertSelective(ROLE record); |
| 11 | 15 | ||
| 12 | int setRolePermissoin(RolePermission record); | 16 | int setRolePermissoin(RolePermission record); |
| @@ -46,6 +46,12 @@ public class RoleServiceImp implements RoleService{ | @@ -46,6 +46,12 @@ public class RoleServiceImp implements RoleService{ | ||
| 46 | return roleMapper.insertSelective(record); | 46 | return roleMapper.insertSelective(record); |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | + @Override | ||
| 50 | + public List<ROLE> getROLE_anonymousPermList() { | ||
| 51 | + List<ROLE> list = roleMapper.findAll("ROLE_anonymous", null); | ||
| 52 | + return list; | ||
| 53 | + } | ||
| 54 | + | ||
| 49 | @Transactional(rollbackFor = Exception.class) | 55 | @Transactional(rollbackFor = Exception.class) |
| 50 | @Override | 56 | @Override |
| 51 | public int setRolePermissoin(RolePermission record){ | 57 | public int setRolePermissoin(RolePermission record){ |
-
请 注册 或 登录 后发表评论