diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/AuthConfig.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/AuthConfig.java index 1cd0a8f..cad9dd4 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/AuthConfig.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/AuthConfig.java @@ -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(); - } - } diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/Constant.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/Constant.java index 3c788d8..95d5e5c 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/Constant.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/Constant.java @@ -28,6 +28,11 @@ public class Constant { */ public static final String HEADER_USER_TYPE = "userType"; + /** + * 描述 : 头信息(token) + */ + public static final String HEADER_ACCESS_TOKEN = "accessToken"; + /** * 身份认证顺序 */ diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/IUserAuthenticationService.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/IUserAuthenticationService.java index 72fb7a2..c4cc464 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/IUserAuthenticationService.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/IUserAuthenticationService.java @@ -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 { * 在单例登录的情况下,需要处理强制登录的情况 *

* - * @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校验 + *

+ * 需要处理单例登录的情况 + *

+ *

+ * 在单例登录的情况下,需要处理强制登录的情况 + *

+ * + * @param accessToken accessToken + */ + void verification(String accessToken); } diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/UserType.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/UserType.java index 5ba167e..bb23de7 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/UserType.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/UserType.java @@ -22,14 +22,14 @@ public enum UserType { /** * mappings */ - private static final Map mappings = new HashMap<>(); + private static final Map 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); } /** diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/component/DefUserAuthenticationService.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/component/DefUserAuthenticationService.java deleted file mode 100644 index e3fe27c..0000000 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/component/DefUserAuthenticationService.java +++ /dev/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; - } -} diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationModel.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationModel.java index 2fce72b..de2dc2c 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationModel.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationModel.java @@ -8,6 +8,9 @@ import org.springframework.validation.annotation.Validated; import java.io.Serializable; +/** + * UserAuthenticationModel + */ @ApiModel(description = "用户鉴权输入模型") @Data @Validated diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationResult.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationResult.java index 59a75b0..698e7cf 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationResult.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/domain/UserAuthenticationResult.java @@ -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 diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/AbstractBaseZuulFilter.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/AbstractBaseZuulFilter.java index 6ebbd41..dbd7a6b 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/AbstractBaseZuulFilter.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/AbstractBaseZuulFilter.java @@ -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 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; } } diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/client/ClientIdentityFilter.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/client/ClientIdentityFilter.java index 3965fcf..562ad59 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/client/ClientIdentityFilter.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/filter/client/ClientIdentityFilter.java @@ -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; } diff --git a/udf-starter-auth/src/main/java/org/itkk/udf/auth/web/UserAuthenticationController.java b/udf-starter-auth/src/main/java/org/itkk/udf/auth/web/UserAuthenticationController.java index 4215d7a..7846724 100644 --- a/udf-starter-auth/src/main/java/org/itkk/udf/auth/web/UserAuthenticationController.java +++ b/udf-starter-auth/src/main/java/org/itkk/udf/auth/web/UserAuthenticationController.java @@ -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 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()))); } }