TokenController.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.yihu.base.security.controller;
  2. import com.yihu.base.security.rbas.ClientServiceProvider;
  3. import com.yihu.base.security.vo.BaseEnvelop;
  4. import com.yihu.base.security.vo.BaseSecurityRequestMapping;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.authentication.AuthenticationManager;
  7. import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
  8. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.text.ParseException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. /**
  19. * Created by 刘文彬 on 2018/5/4.
  20. */
  21. @RestController
  22. @RequestMapping("/tokens")
  23. //@Api(value = "权限token模块", description = "权限token模块")
  24. public class TokenController {
  25. @Autowired
  26. private DefaultTokenServices defaultTokenServices;
  27. @Autowired
  28. private AuthenticationManager authenticationManager;
  29. @Autowired
  30. private ClientServiceProvider clientDetailsService;
  31. // @Autowired
  32. // private EmployFeign employFeign;
  33. private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  34. @PostMapping(BaseSecurityRequestMapping.BaseToken.api_update_token_expiration_time)
  35. // @ApiOperation(value = "更新token过期时间", notes = "根据token更新token过期时间")
  36. public BaseEnvelop updateTokenExpiration(
  37. // @ApiParam(name = "expiration", value = "token过期时间,格式:yyyy-mm-dd HH:mm:ss", required = true)
  38. @RequestParam(value = "expiration", required = true) String expiration,
  39. // @ApiParam(name = "authHeaderValue", value = "登录的token", required = true)
  40. @RequestParam(value = "authHeaderValue", required = true) String authHeaderValue) throws ParseException {
  41. DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken)defaultTokenServices.readAccessToken(authHeaderValue);
  42. if(token!=null){
  43. try {
  44. token.setExpiration(formatter.parse(expiration));
  45. } catch (ParseException e) {
  46. e.printStackTrace();
  47. }
  48. return BaseEnvelop.getSuccess("token 过期时间设置成功!");
  49. }
  50. return BaseEnvelop.getError("token 无效!");
  51. }
  52. @PostMapping(BaseSecurityRequestMapping.BaseToken.api_update_token_expiration_second)
  53. // @ApiOperation(value = "延长token的过期时间", notes = "根据token延长token的过期时间")
  54. public BaseEnvelop updateTokenExpiration(
  55. // @ApiParam(name = "seconds", value = "延长token过期时间分钟数", required = true)
  56. @RequestParam(value = "seconds", required = true) int seconds,
  57. // @ApiParam(name = "authHeaderValue", value = "登录的token", required = true)
  58. @RequestParam(value = "authHeaderValue", required = true) String authHeaderValue){
  59. DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken)defaultTokenServices.readAccessToken(authHeaderValue);
  60. if(token!=null){
  61. token.setExpiration(new Date(System.currentTimeMillis() + (seconds * 1000L)));
  62. return BaseEnvelop.getSuccess("token 过期时间设置成功!");
  63. }
  64. return BaseEnvelop.getError("token 无效!");
  65. }
  66. @PostMapping(BaseSecurityRequestMapping.BaseToken.api_update_token_expiration)
  67. // @ApiOperation(value = "设置token过期", notes = "设置token过期")
  68. public BaseEnvelop updateTokenExpiration2(HttpServletRequest request, HttpServletResponse response,
  69. // @ApiParam(name = "authHeaderValue", value = "登录的token", required = true)
  70. @RequestParam(value = "authHeaderValue", required = true) String authHeaderValue){
  71. DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken)defaultTokenServices.readAccessToken(authHeaderValue);
  72. if(token!=null){
  73. token.setExpiration(new Date());
  74. return BaseEnvelop.getSuccess("token 过期时间设置成功!");
  75. }
  76. return BaseEnvelop.getError("token 无效!");
  77. }
  78. // @PostMapping(BaseSecurityRequestMapping.BaseToken.api_update_token_expiration_second2)
  79. // @ApiOperation(value = "设置token过期", notes = "设置token过期")
  80. // public BaseEnvelop updateTokenExpiration3(HttpServletRequest request, HttpServletResponse response,
  81. // @ApiParam(name = "mobileSaas", value = "mobile和saas组合,逗号分隔", required = true)
  82. // @RequestParam(value = "mobileSaas", required = true) String mobileSaas){
  83. //
  84. // String[] sp = mobileSaas.split(",");
  85. // String phone = sp[0];
  86. // String saasId = sp[1];
  87. // Envelop envelop = employFeign.getEmployeeByPhoneAndSaasId(phone,saasId);
  88. // Map baseEmployDO = (Map)envelop.getObj();
  89. // if(baseEmployDO.isEmpty()){
  90. // return BaseEnvelop.getError("该用户不存在!");
  91. // }
  92. // UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
  93. // mobileSaas, baseEmployDO.get("password"));
  94. //
  95. // // Allow subclasses to set the "details" property
  96. // authRequest.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
  97. //
  98. // Authentication authentication = authenticationManager.authenticate(authRequest);
  99. // ClientDetails clientDetails = clientDetailsService.loadClientByClientId(saasId);
  100. // TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, saasId, clientDetails.getScope(), "custom_password");
  101. //
  102. // OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
  103. //
  104. // OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
  105. //
  106. // DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken)defaultTokenServices.getAccessToken(oAuth2Authentication);
  107. // if(token!=null){
  108. // token.setExpiration(new Date());
  109. // return BaseEnvelop.getSuccess("token 过期时间设置成功!");
  110. // }
  111. // return BaseEnvelop.getError("token 无效!");
  112. // }
  113. }