添加filter(未实现)

This commit is contained in:
wangkang
2017-08-21 11:09:45 +08:00
parent 0f068e5838
commit ad5369b0d7
10 changed files with 88 additions and 47 deletions
@@ -1,8 +1,5 @@
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;
/**
@@ -11,15 +8,4 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class AuthConfig {
/**
* defUserAuthenticationService
*
* @return IUserAuthenticationService
*/
@Bean
@ConditionalOnMissingBean(IUserAuthenticationService.class)
public IUserAuthenticationService defUserAuthenticationService() {
return new DefUserAuthenticationService();
}
}
@@ -28,6 +28,11 @@ public class Constant {
*/
public static final String HEADER_USER_TYPE = "userType";
/**
* 描述 : 头信息(token)
*/
public static final String HEADER_ACCESS_TOKEN = "accessToken";
/**
* 身份认证顺序
*/
@@ -1,8 +1,8 @@
package org.itkk.udf.auth;
import org.itkk.udf.auth.domain.UserAuthenticationModel;
import org.itkk.udf.auth.domain.UserAuthenticationResult;
/**
* IUserAuthenticationService
*/
public interface IUserAuthenticationService {
/**
@@ -14,9 +14,25 @@ public interface IUserAuthenticationService {
* 在单例登录的情况下,需要处理强制登录的情况
* </p>
*
* @param model 用户鉴权输入模型
* @return 用户鉴权响应模型
* @param account 账号
* @param password 密码
* @param singleton 是否单例
* @param forced 是否强制
* @return accessToken
*/
UserAuthenticationResult authentication(UserAuthenticationModel model);
String authentication(String account, String password, boolean singleton, boolean forced);
/**
* toke校验
* <p>
* 需要处理单例登录的情况
* </p>
* <p>
* 在单例登录的情况下,需要处理强制登录的情况
* </p>
*
* @param accessToken accessToken
*/
void verification(String accessToken);
}
@@ -22,14 +22,14 @@ public enum UserType {
/**
* mappings
*/
private static final Map<String, UserType> mappings = new HashMap<>();
private static final Map<String, UserType> MAPPINGS = new HashMap<>();
/**
* 静态块
*/
static {
for (UserType userType : values()) {
mappings.put(userType.name(), userType);
MAPPINGS.put(userType.name(), userType);
}
}
@@ -40,7 +40,7 @@ public enum UserType {
* @return 枚举
*/
public static UserType resolve(String userType) {
return (userType != null ? mappings.get(userType) : null);
return (userType != null ? MAPPINGS.get(userType) : null);
}
/**
@@ -1,20 +0,0 @@
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;
import java.util.UUID;
/**
* DefUserAuthenticationService
*/
public class DefUserAuthenticationService implements IUserAuthenticationService {
@Override
public UserAuthenticationResult authentication(UserAuthenticationModel model) {
UserAuthenticationResult userAuthenticationResult = new UserAuthenticationResult();
userAuthenticationResult.setAccessToken(UUID.randomUUID().toString());
return userAuthenticationResult;
}
}
@@ -8,6 +8,9 @@ import org.springframework.validation.annotation.Validated;
import java.io.Serializable;
/**
* UserAuthenticationModel
*/
@ApiModel(description = "用户鉴权输入模型")
@Data
@Validated
@@ -2,10 +2,15 @@ package org.itkk.udf.auth.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* UserAuthenticationResult
*/
@ApiModel(description = "用户鉴权响应模型")
@Data
@AllArgsConstructor
public class UserAuthenticationResult {
/**
* 授权token
@@ -29,6 +29,28 @@ public abstract class AbstractBaseZuulFilter extends ZuulFilter {
@Autowired
private AuthProperties authProperties;
/**
* 获得token
*
* @return token
*/
public String getAccessToken() {
//获得request
RequestContext ctx = getCurrentContext();
HttpServletRequest request = ctx.getRequest();
//获得accessToken
String accessToken = request.getHeader(Constant.HEADER_ACCESS_TOKEN);
if (StringUtils.isBlank(accessToken)) {
accessToken = request.getParameter(Constant.HEADER_ACCESS_TOKEN);
}
//判空
if (StringUtils.isBlank(accessToken)) {
throw new AuthException("missing required authentication parameters (accessToken)");
}
//返回
return accessToken;
}
/**
* 判断排除
*
@@ -54,7 +76,7 @@ public abstract class AbstractBaseZuulFilter extends ZuulFilter {
Iterator<String> keys = map.keySet().iterator();
while (keys.hasNext()) {
ExcludeServiceMeta item = map.get(keys.next());
if (item.getUrl().equals(url) && item.getMethod().equals(method)) {
if (item.getUrl().startsWith(url) && item.getMethod().equals(method)) {
return false;
}
}
@@ -2,23 +2,41 @@ package org.itkk.udf.auth.filter.client;
import lombok.extern.slf4j.Slf4j;
import org.itkk.udf.auth.Constant;
import org.itkk.udf.auth.IUserAuthenticationService;
import org.itkk.udf.auth.UserType;
import org.itkk.udf.auth.filter.AbstractBaseZuulFilter;
import org.itkk.udf.core.exception.SystemRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
/**
* IdentityFilter
* ClientIdentityFilter
*/
@Component
@Slf4j
@ConditionalOnProperty(value = "org.itkk.auth.filter.client.identity.enabled", matchIfMissing = true)
public class ClientIdentityFilter extends AbstractBaseZuulFilter {
/**
* userAuthenticationService
*/
@Autowired(required = false)
private IUserAuthenticationService userAuthenticationService;
@Override
public Object run() {
log.info("ClientIdentityFilter.run");
//判空
if (userAuthenticationService == null) {
throw new SystemRuntimeException("You need to implement the IUserAuthenticationService interface");
}
//获得accessToken
String accessToken = this.getAccessToken();
//校验token
userAuthenticationService.verification(accessToken);
//返回
return null;
}
@@ -1,9 +1,11 @@
package org.itkk.udf.auth.web;
import lombok.extern.slf4j.Slf4j;
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.itkk.udf.core.exception.SystemRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@@ -12,15 +14,19 @@ import org.springframework.web.bind.annotation.RestController;
* UserAuthenticationController
*/
@RestController
@Slf4j
public class UserAuthenticationController implements IUserAuthenticationController {
/**
* userAuthenticationService
*/
@Autowired
@Autowired(required = false)
private IUserAuthenticationService userAuthenticationService;
@Override
public RestResponse<UserAuthenticationResult> authentication(@RequestBody UserAuthenticationModel model) {
return new RestResponse<>(userAuthenticationService.authentication(model));
if (userAuthenticationService == null) {
throw new SystemRuntimeException("You need to implement the IUserAuthenticationService interface");
}
return new RestResponse<>(new UserAuthenticationResult(userAuthenticationService.authentication(model.getAccount(), model.getPassword(), model.isSingleton(), model.isForced())));
}
}