Commit 44b0c1af by naan1993

完善验证器

parent 40499458
......@@ -2,18 +2,16 @@ package com.stylefeng.guns.rest.modular.auth.controller;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.modular.auth.controller.dto.AuthRequest;
import com.stylefeng.guns.rest.modular.auth.controller.dto.AuthResponse;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.auth.validator.IReqValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* 请求验证的
......@@ -31,13 +29,13 @@ public class AuthController {
private IReqValidator reqValidator;
@RequestMapping(value = "${jwt.auth-path}")
public ResponseEntity<?> createAuthenticationToken(@RequestParam Map<String, Object> params, HttpServletRequest request) {
public ResponseEntity<?> createAuthenticationToken(AuthRequest authRequest) {
boolean validate = reqValidator.validate(params);
boolean validate = reqValidator.validate(authRequest);
if (validate) {
final String randomKey = jwtTokenUtil.getRandomKey();
final String token = jwtTokenUtil.generateToken((String) params.get("userName"), randomKey);
final String token = jwtTokenUtil.generateToken(authRequest.getUserName(), randomKey);
return ResponseEntity.ok(new AuthResponse(token, randomKey));
} else {
throw new BussinessException(BizExceptionEnum.AUTH_REQUEST_ERROR);
......
package com.stylefeng.guns.rest.modular.auth.controller.dto;
import java.io.Serializable;
import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
/**
* 认证的请求dto
......@@ -8,35 +8,34 @@ import java.io.Serializable;
* @author fengshuonan
* @Date 2017/8/24 14:00
*/
public class AuthRequest implements Serializable {
public class AuthRequest implements Credence {
private static final long serialVersionUID = -8445943548965154778L;
private String username;
private String userName;
private String password;
public AuthRequest() {
super();
public void setUserName(String userName) {
this.userName = userName;
}
public AuthRequest(String username, String password) {
this.setUsername(username);
this.setPassword(password);
public String getPassword() {
return password;
}
public String getUsername() {
return this.username;
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
public String getUserName() {
return userName;
}
public String getPassword() {
return this.password;
@Override
public String getCredenceName() {
return this.userName;
}
public void setPassword(String password) {
this.password = password;
@Override
public String getCredenceCode() {
return this.password;
}
}
package com.stylefeng.guns.rest.modular.auth.validator;
import java.util.Map;
import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
/**
* <p>验证请求/auth接口时,请求参数的正确性</p>
*
* <p>
* <p>如果想拓展验证方法只需实现这个接口,然后在AuthenticationRestController类中注意相应实现的本接口的类即可</p>
*
* @author fengshuonan
......@@ -18,5 +18,5 @@ public interface IReqValidator {
* @author fengshuonan
* @Date 2017/8/23 11:49
*/
boolean validate(Map<String, Object> params);
boolean validate(Credence credence);
}
package com.stylefeng.guns.rest.modular.auth.validator.dto;
/**
* 验证的凭据
*
* @author fengshuonan
* @date 2017-08-27 13:27
*/
public interface Credence {
/**
* 凭据名称
*/
String getCredenceName();
/**
* 密码或者是其他的验证码之类的
*/
String getCredenceCode();
}
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.rest.common.persistence.dao.UserMapper;
import com.stylefeng.guns.rest.common.persistence.model.User;
import com.stylefeng.guns.rest.modular.auth.validator.IReqValidator;
import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -23,11 +24,11 @@ public class DbValidator implements IReqValidator {
UserMapper userMapper;
@Override
public boolean validate(Map<String, Object> params) {
List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("userName", params.get("userName")));
if(users != null && users.size() > 0){
public boolean validate(Credence credence) {
List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("userName", credence.getCredenceName()));
if (users != null && users.size() > 0) {
return true;
}else{
} else {
return false;
}
}
......
package com.stylefeng.guns.rest.modular.auth.validator.impl;
import com.stylefeng.guns.rest.modular.auth.validator.IReqValidator;
import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* 直接验证账号密码是不是admin
*
......@@ -19,14 +18,14 @@ public class SimpleValidator implements IReqValidator {
private static String PASSWORD = "admin";
@Override
public boolean validate(Map<String, Object> params) {
public boolean validate(Credence credence) {
String userName = (String) params.get("userName");
String password = (String) params.get("password");
String userName = credence.getCredenceName();
String password = credence.getCredenceCode();
if(USER_NAME.equals(userName) && PASSWORD.equals(password)){
if (USER_NAME.equals(userName) && PASSWORD.equals(password)) {
return true;
}else{
} else {
return false;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment