Просмотр исходного кода

[后台添加验证码][新增]:验证码添加

release_toaliyun_real
Stormeye.Wu 7 лет назад
Родитель
Сommit
bbb8a85d9d
6 измененных файлов: 144 добавлений и 5 удалений
  1. +32
    -0
      mallinkAdmin/src/main/java/com/simple/config/KaptchaConfig.java
  2. +1
    -0
      mallinkAdmin/src/main/java/com/simple/config/ShiroConfig.java
  3. +42
    -5
      mallinkAdmin/src/main/java/com/simple/controller/HomeController.java
  4. +61
    -0
      mallinkAdmin/src/main/java/com/simple/utils/ShiroUtils.java
  5. +2
    -0
      mallinkService/src/main/java/com/simple/common/ErrorCode.java
  6. +6
    -0
      pom.xml

+ 32
- 0
mallinkAdmin/src/main/java/com/simple/config/KaptchaConfig.java Просмотреть файл

@@ -0,0 +1,32 @@
package com.simple.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;


/**
* 生成验证码配置
*
* @author stormeye.wu
* @email wugq@mippoint.com
* @date 2017-04-20 19:22
*/
@Configuration
public class KaptchaConfig {

@Bean
public DefaultKaptcha producer() {
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.textproducer.font.color", "black");
properties.put("kaptcha.textproducer.char.space", "5");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}

+ 1
- 0
mallinkAdmin/src/main/java/com/simple/config/ShiroConfig.java Просмотреть файл

@@ -120,6 +120,7 @@ public class ShiroConfig {
filterChainDefinitionMap.put("/carCallback/**","anon");
filterChainDefinitionMap.put("/wxMallApply/add","anon");
filterChainDefinitionMap.put("/wxMallApply/sendvalidationcode","anon");
filterChainDefinitionMap.put("/captcha.jpg", "anon");
// filterChainDefinitionMap.put("/role/**", "corsFilter,token");
filterChainDefinitionMap.put("/**", "corsFilter,token,authc");
// filterChainDefinitionMap.put("/**", "anon");


+ 42
- 5
mallinkAdmin/src/main/java/com/simple/controller/HomeController.java Просмотреть файл

@@ -1,6 +1,9 @@
package com.simple.controller;

import com.github.pagehelper.PageInfo;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.simple.common.ErrorCode;
import com.simple.common.ResultData;
import com.simple.domain.po.MallRolePermission;
import com.simple.domain.po.MallUserInfo;
@@ -8,8 +11,10 @@ import com.simple.domain.po.MallUserRole;
import com.simple.service.MallRolePermissionService;
import com.simple.service.MallUserRoleService;
import com.simple.shiro.UserSession;
import com.simple.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
@@ -17,10 +22,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;


@RestController
@@ -28,15 +37,43 @@ import org.springframework.web.bind.annotation.RestController;
public class HomeController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());


@Autowired
private Producer producer;

@Autowired
private MallUserRoleService mallUserRoleService;

@Autowired
private MallRolePermissionService mallRolePermissionService;

@RequestMapping("/captcha.jpg")
public void captcha(HttpServletResponse response)throws ServletException, IOException {
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");

//生成文字验证码
String text = producer.createText();
//生成图片验证码
BufferedImage image = producer.createImage(text);
//保存到shiro session
ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "jpg", out);
IOUtils.closeQuietly(out);
}

@ApiOperation("登录")
@PostMapping("/doLogin")
public ResultData login(@RequestBody MallUserInfo user) {
public ResultData login(@RequestBody MallUserInfo user, String captcha) {
if (captcha != null) {
String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
if(!captcha.equalsIgnoreCase(kaptcha)){
return new ResultData(ErrorCode.KAPCHA_NOT_EQUAL);
}
}

ResultData data = new ResultData();
if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
// throw new SystemException(ErrorCode.LOGIN_USER_OR_PWD_ERROR);


+ 61
- 0
mallinkAdmin/src/main/java/com/simple/utils/ShiroUtils.java Просмотреть файл

@@ -0,0 +1,61 @@
package com.simple.utils;

import com.simple.common.ErrorCode;
import com.simple.domain.po.MallUserInfo;
import com.simple.exception.MallinkException;
import com.simple.shiro.UserSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
* Shiro工具类
*
* @author stormeye.wu
* @email wugq@mippoint.com
* @date 2016年11月12日 上午9:49:19
*/
public class ShiroUtils {

public static Session getSession() {
return SecurityUtils.getSubject().getSession();
}

public static Subject getSubject() {
return SecurityUtils.getSubject();
}

public static MallUserInfo getUserInfo() {
return (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo);
}

public static Long getUserId() {
return getUserInfo().getId();
}

public static void setSessionAttribute(Object key, Object value) {
getSession().setAttribute(key, value);
}

public static Object getSessionAttribute(Object key) {
return getSession().getAttribute(key);
}

public static boolean isLogin() {
return SecurityUtils.getSubject().getPrincipal() != null;
}

public static void logout() {
SecurityUtils.getSubject().logout();
}

public static String getKaptcha(String key) {
Object kaptcha = getSessionAttribute(key);
if (kaptcha == null) {
throw new MallinkException(ErrorCode.KAPCHA_NOT_VALID);
}
getSession().removeAttribute(key);
return kaptcha.toString();
}

}

+ 2
- 0
mallinkService/src/main/java/com/simple/common/ErrorCode.java Просмотреть файл

@@ -54,6 +54,8 @@ public enum ErrorCode{
USER_IS_LOCKED(2003, "用户已经被锁定不能登录,请与管理员联系"),
NEW_USER_FAILD(2004, "创建新用户失败"),
BUSER_NOT_IN_APP(2005, "用户不是此app用户"),
KAPCHA_NOT_VALID(2006, "验证码已失效"),
KAPCHA_NOT_EQUAL(2007, "验证码不正确"),

/**
* 商场/商户


+ 6
- 0
pom.xml Просмотреть файл

@@ -222,6 +222,12 @@
<version>2.8.5</version>
</dependency>

<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>

<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>


Загрузка…
Отмена
Сохранить