This commit is contained in:
王康
2017-08-20 17:37:27 +08:00
parent 0e83336379
commit 53b307222b
15 changed files with 278 additions and 6 deletions
@@ -1,5 +1,8 @@
package org.itkk.udf.auth;
import org.itkk.udf.auth.component.DefUserAuthenticationService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
@@ -8,4 +11,15 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class AuthConfig {
/**
* defUserAuthenticationService
*
* @return IUserAuthenticationService
*/
@Bean
@ConditionalOnMissingBean(IUserAuthenticationService.class)
public IUserAuthenticationService defUserAuthenticationService() {
return new DefUserAuthenticationService();
}
}
@@ -0,0 +1,45 @@
package org.itkk.udf.auth;
import lombok.Data;
import org.itkk.udf.auth.meta.ExcludeServiceMeta;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* AuthProperties
*/
@Component
@Data
@ConfigurationProperties(prefix = "org.itkk.auth.properties")
public class AuthProperties {
/**
* 是否单例登录
*/
private boolean singletonLogin = false;
/**
* 用户服务排除
*/
private Map<String, ExcludeServiceMeta> userExcludeService;
/**
* 客户端服务排除
*/
private Map<String, ExcludeServiceMeta> clientExcludeServiceMeta;
/**
* 构造方法
*/
public AuthProperties() {
userExcludeService = new HashMap<>();
ExcludeServiceMeta authentication = new ExcludeServiceMeta();
authentication.setMethod(Constant.USER_AUTHENTICATION_METHOD);
authentication.setUrl(Constant.USER_AUTHENTICATION_URL);
userExcludeService.put("AUTH_USER_1", authentication);
}
}
@@ -14,7 +14,17 @@ package org.itkk.udf.auth;
public class Constant {
/**
* 描述 : 头信息(rms应用名称)
* 描述 : 用户鉴权url
*/
public static final String USER_AUTHENTICATION_URL = "/service/user";
/**
* 描述 : 用户鉴权http方法
*/
public static final String USER_AUTHENTICATION_METHOD = "POST";
/**
* 描述 : 头信息(用户类型)
*/
public static final String HEADER_USER_TYPE = "userType";
@@ -33,7 +43,6 @@ public class Constant {
*/
public static final int ORDER_RMS = 10002;
/**
* 描述 : 私有化构造函数
*/
@@ -0,0 +1,16 @@
package org.itkk.udf.auth;
import org.itkk.udf.auth.domain.UserAuthenticationModel;
import org.itkk.udf.auth.domain.UserAuthenticationResult;
public interface IUserAuthenticationService {
/**
* 用户鉴权
*
* @param model 用户鉴权输入模型
* @return 用户鉴权响应模型
*/
UserAuthenticationResult authentication(UserAuthenticationModel model);
}
@@ -0,0 +1,16 @@
package org.itkk.udf.auth.component;
import org.itkk.udf.auth.IUserAuthenticationService;
import org.itkk.udf.auth.domain.UserAuthenticationModel;
import org.itkk.udf.auth.domain.UserAuthenticationResult;
/**
* DefUserAuthenticationService
*/
public class DefUserAuthenticationService implements IUserAuthenticationService {
@Override
public UserAuthenticationResult authentication(UserAuthenticationModel model) {
return null;
}
}
@@ -0,0 +1,40 @@
package org.itkk.udf.auth.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import java.io.Serializable;
@ApiModel(description = "用户鉴权输入模型")
@Data
@Validated
public class UserAuthenticationModel implements Serializable {
/**
* 描述 : ID
*/
private static final long serialVersionUID = 1L;
/**
* 用户名
*/
@ApiModelProperty(value = "用户名", required = true, dataType = "string")
@NotBlank
private String userName;
/**
* 密码
*/
@ApiModelProperty(value = "密码", required = true, dataType = "string")
@NotBlank
private String password;
/**
* 强制登录(只在禁止同时登录的模式下有效)
*/
@ApiModelProperty(value = "强制登录(只在禁止同时登录的模式下有效)", required = false, dataType = "boolean")
private boolean forced = false;
}
@@ -0,0 +1,15 @@
package org.itkk.udf.auth.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(description = "用户鉴权响应模型")
@Data
public class UserAuthenticationResult {
/**
* 授权token
*/
@ApiModelProperty(value = "授权token", required = true, dataType = "string")
private String accessToken;
}
@@ -2,12 +2,18 @@ package org.itkk.udf.auth.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.itkk.udf.auth.AuthProperties;
import org.itkk.udf.auth.Constant;
import org.itkk.udf.auth.UserType;
import org.itkk.udf.auth.meta.ExcludeServiceMeta;
import org.itkk.udf.core.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
@@ -15,6 +21,42 @@ import static com.netflix.zuul.context.RequestContext.getCurrentContext;
* AbstractBaseZuulFilter
*/
public abstract class AbstractBaseZuulFilter extends ZuulFilter {
/**
* authProperties
*/
@Autowired
private AuthProperties authProperties;
/**
* 判断排除
*
* @param targetUserType 目标用户类型
* @return 是否匹配
*/
public boolean checkExclude(UserType targetUserType) {
//获得request
RequestContext ctx = getCurrentContext();
HttpServletRequest request = ctx.getRequest();
//获得url和method
String url = request.getRequestURI();
String method = request.getMethod();
//获得排除列表
Map<String, ExcludeServiceMeta> map = targetUserType.equals(UserType.USER) ? authProperties.getUserExcludeService() : targetUserType.equals(UserType.CLIENT) ? authProperties.getClientExcludeServiceMeta() : null;
//判空 & 遍历 & 匹配排除规则
if (MapUtils.isNotEmpty(map)) {
Iterator<String> keys = map.keySet().iterator();
while (keys.hasNext()) {
ExcludeServiceMeta item = map.get(keys.next());
if (item.getUrl().equals(url) && item.getMethod().equals(method)) {
return false;
}
}
}
//返回
return true;
}
/**
* 判断用户类型
*
@@ -24,7 +24,7 @@ public class ClientIdentityFilter extends AbstractBaseZuulFilter {
@Override
public boolean shouldFilter() {
return this.checkUserType(UserType.CLIENT);
return this.checkUserType(UserType.CLIENT) && this.checkExclude(UserType.CLIENT);
}
@Override
@@ -24,7 +24,7 @@ public class ClientPurviewFilter extends AbstractBaseZuulFilter {
@Override
public boolean shouldFilter() {
return this.checkUserType(UserType.CLIENT);
return this.checkUserType(UserType.CLIENT) && this.checkExclude(UserType.CLIENT);
}
@Override
@@ -24,7 +24,7 @@ public class UserIdentityFilter extends AbstractBaseZuulFilter {
@Override
public boolean shouldFilter() {
return this.checkUserType(UserType.USER);
return this.checkUserType(UserType.USER) && this.checkExclude(UserType.USER);
}
@Override
@@ -24,7 +24,7 @@ public class UserPurviewFilter extends AbstractBaseZuulFilter {
@Override
public boolean shouldFilter() {
return this.checkUserType(UserType.USER);
return this.checkUserType(UserType.USER) && this.checkExclude(UserType.USER);
}
@Override
@@ -0,0 +1,19 @@
package org.itkk.udf.auth.meta;
import lombok.Data;
/**
* ExcludeServiceMeta
*/
@Data
public class ExcludeServiceMeta {
/**
* url
*/
private String url;
/**
* 请求方法
*/
private String method;
}
@@ -0,0 +1,30 @@
package org.itkk.udf.auth.web;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.itkk.udf.auth.Constant;
import org.itkk.udf.auth.domain.UserAuthenticationModel;
import org.itkk.udf.auth.domain.UserAuthenticationResult;
import org.itkk.udf.core.RestResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* IUserAuthenticationController
*/
@Api(value = "用户鉴权服务", consumes = "application/json", produces = "application/json", protocols = "http")
@RequestMapping(value = Constant.USER_AUTHENTICATION_URL)
public interface IUserAuthenticationController { //NOSONAR
/**
* 用户鉴权
*
* @param model 用户鉴权输入模型
* @return 用户鉴权响应模型
*/
@ApiOperation(value = "AUTH_USER_1", notes = "用户鉴权")
@PostMapping
RestResponse<UserAuthenticationResult> authentication(@RequestBody UserAuthenticationModel model);
}
@@ -0,0 +1,26 @@
package org.itkk.udf.auth.web;
import org.itkk.udf.auth.IUserAuthenticationService;
import org.itkk.udf.auth.domain.UserAuthenticationModel;
import org.itkk.udf.auth.domain.UserAuthenticationResult;
import org.itkk.udf.core.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* UserAuthenticationController
*/
@RestController
public class UserAuthenticationController implements IUserAuthenticationController {
/**
* userAuthenticationService
*/
@Autowired
private IUserAuthenticationService userAuthenticationService;
@Override
public RestResponse<UserAuthenticationResult> authentication(@RequestBody UserAuthenticationModel model) {
return new RestResponse<>(userAuthenticationService.authentication(model));
}
}