開發一個博客系統文章數:菜單文章都沒更新完
搭建eureka註冊中心 (請先閱讀此文章) https://segmentfault.com/a/11...
搭建Beans編寫Token解析註解 https://segmentfault.com/a/11...
搭建權限管理系統 https://segmentfault.com/a/11...
搭建後臺管理系統 https://segmentfault.com/a/11...
前端Fetch請求封裝 https://segmentfault.com/a/11...前端
1.建立一個maven的空項目,寫本身的Entity和工具類在打包以後在其餘項目中引入 就能夠了;java
IDEA打包部署項目到maven
在Idea中的最右邊的導航欄中能夠看到一個Maven Projects,單擊打開,如圖所示:
web
引入Beans項目到當前項目:
<dependency> <groupId>com.server</groupId> <artifactId>beans</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
編寫Token解析註解(Token的字符串-解析爲->爲TokenEntity實體)
註解解析Token實例spring
TokenEntity實體 public class TokenEntity{ private String userId; private String token; private Date expiresDate; //過時時間 private Date createDate; //建立時間 public TokenEntity(String userId, String token, Date expiresDate, Date createDate) { this.userId = userId; this.token = token; this.expiresDate = expiresDate; this.createDate = createDate; } public TokenEntity() { super(); } get/set方法略
解析Token的工具類中的方法(此方法根據本身具體如何解析token來編寫) public static Object getToken(String token,String key) throws Exception { DecodedJWT jwt = verifyToken(token,key); Map<String, Claim> map = jwt.getClaims(); TokenEntity bean = new TokenEntity(); bean.setUserId(map.get("userId").asString()); bean.setToken(token); bean.setExpiresDate(jwt.getIssuedAt()); bean.setCreateDate(jwt.getExpiresAt()); return bean; }
主要註解相關內容來了
註解類 import java.lang.annotation.*; @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestToken { String value(); }
解析類 import com.server.authority.util.TokenUtil; import com.server.config.SysBaseConfig; import org.springframework.core.MethodParameter; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; public class RequestTokenAnalysis implements HandlerMethodArgumentResolver{ @Override public boolean supportsParameter(MethodParameter methodParameter) { return methodParameter.hasParameterAnnotation(RequestToken.class); } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception { RequestToken token = methodParameter.getParameterAnnotation(RequestToken.class); String value = token.value(); Class clazz = methodParameter.getParameterType(); String data = nativeWebRequest.getParameter(value); if (data == null) { return clazz.newInstance(); } Object object = TokenUtil.getToken(data, SysBaseConfig.TOKEN_KEY); return object; } }
測試註解類 @RequestMapping(value = "/token",method = RequestMethod.GET) @ResponseBody public TResponse userToken(@RequestToken(ParameterNames.ACCESS_TOKEN) TokenEntity token){ try{ return authorityService.userVerify(token.getToken()); }catch (Exception e){ return tResponse.getTResponse("oxs-pai-appMenu","zh_CN", e.getMessage(), SResponse.getInstance().getSResponse(StatusCode.USER_DEFINED,"異常")); } }
例如地址:http://127.0.0.1:8762/auth/token 請求頭中添加:at=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MzI2NTI3MTIsInVzZXJJZCI6IjEiLCJpYXQiOjE1MzI1NjYzMTJ9.z1MweHILT6f2KeUumVSNuuY0F9EYsePQp5JmKJiwDuc
注入註解 import com.server.authority.annotation.RequestTokenAnalysis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.List; @SpringBootApplication @EnableEurekaClient public class AuthorityApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(AuthorityApplication.class, args); } @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(new RequestTokenAnalysis()); super.addArgumentResolvers(argumentResolvers); } }
總結:可以成功解析出TokenEntity