Commit 44b0c1af by naan1993

完善验证器

parent 40499458
...@@ -2,18 +2,16 @@ package com.stylefeng.guns.rest.modular.auth.controller; ...@@ -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.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException; 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.controller.dto.AuthResponse;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil; import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.auth.validator.IReqValidator; import com.stylefeng.guns.rest.modular.auth.validator.IReqValidator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/** /**
* 请求验证的 * 请求验证的
...@@ -31,13 +29,13 @@ public class AuthController { ...@@ -31,13 +29,13 @@ public class AuthController {
private IReqValidator reqValidator; private IReqValidator reqValidator;
@RequestMapping(value = "${jwt.auth-path}") @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) { if (validate) {
final String randomKey = jwtTokenUtil.getRandomKey(); 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)); return ResponseEntity.ok(new AuthResponse(token, randomKey));
} else { } else {
throw new BussinessException(BizExceptionEnum.AUTH_REQUEST_ERROR); throw new BussinessException(BizExceptionEnum.AUTH_REQUEST_ERROR);
......
package com.stylefeng.guns.rest.modular.auth.controller.dto; package com.stylefeng.guns.rest.modular.auth.controller.dto;
import java.io.Serializable; import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
/** /**
* 认证的请求dto * 认证的请求dto
...@@ -8,35 +8,34 @@ import java.io.Serializable; ...@@ -8,35 +8,34 @@ import java.io.Serializable;
* @author fengshuonan * @author fengshuonan
* @Date 2017/8/24 14:00 * @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; private String password;
public AuthRequest() { public void setUserName(String userName) {
super(); this.userName = userName;
} }
public AuthRequest(String username, String password) { public String getPassword() {
this.setUsername(username); return password;
this.setPassword(password);
} }
public String getUsername() { public void setPassword(String password) {
return this.username; this.password = password;
} }
public void setUsername(String username) { public String getUserName() {
this.username = username; return userName;
} }
public String getPassword() { @Override
return this.password; public String getCredenceName() {
return this.userName;
} }
public void setPassword(String password) { @Override
this.password = password; public String getCredenceCode() {
return this.password;
} }
} }
package com.stylefeng.guns.rest.modular.auth.validator; 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>验证请求/auth接口时,请求参数的正确性</p>
* * <p>
* <p>如果想拓展验证方法只需实现这个接口,然后在AuthenticationRestController类中注意相应实现的本接口的类即可</p> * <p>如果想拓展验证方法只需实现这个接口,然后在AuthenticationRestController类中注意相应实现的本接口的类即可</p>
* *
* @author fengshuonan * @author fengshuonan
...@@ -18,5 +18,5 @@ public interface IReqValidator { ...@@ -18,5 +18,5 @@ public interface IReqValidator {
* @author fengshuonan * @author fengshuonan
* @Date 2017/8/23 11:49 * @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; ...@@ -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.dao.UserMapper;
import com.stylefeng.guns.rest.common.persistence.model.User; 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.IReqValidator;
import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -23,11 +24,11 @@ public class DbValidator implements IReqValidator { ...@@ -23,11 +24,11 @@ public class DbValidator implements IReqValidator {
UserMapper userMapper; UserMapper userMapper;
@Override @Override
public boolean validate(Map<String, Object> params) { public boolean validate(Credence credence) {
List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("userName", params.get("userName"))); List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("userName", credence.getCredenceName()));
if(users != null && users.size() > 0){ if (users != null && users.size() > 0) {
return true; return true;
}else{ } else {
return false; return false;
} }
} }
......
package com.stylefeng.guns.rest.modular.auth.validator.impl; 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.IReqValidator;
import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Map;
/** /**
* 直接验证账号密码是不是admin * 直接验证账号密码是不是admin
* *
...@@ -19,14 +18,14 @@ public class SimpleValidator implements IReqValidator { ...@@ -19,14 +18,14 @@ public class SimpleValidator implements IReqValidator {
private static String PASSWORD = "admin"; private static String PASSWORD = "admin";
@Override @Override
public boolean validate(Map<String, Object> params) { public boolean validate(Credence credence) {
String userName = (String) params.get("userName"); String userName = credence.getCredenceName();
String password = (String) params.get("password"); String password = credence.getCredenceCode();
if(USER_NAME.equals(userName) && PASSWORD.equals(password)){ if (USER_NAME.equals(userName) && PASSWORD.equals(password)) {
return true; return true;
}else{ } else {
return false; 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