1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| @Controller public class OauthController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${oauth2.server.url:https://gitlab.com}") private String gitlabServerUrl; @Value("${oauth2.client.id:xxx}") private String clientId; @Value("${oauth2.client.secret:xxxx}") private String clientSecret; @Value("${oauth2.client.callback.url:http://localhost:9000/callback}") private String callbackUrl;
private static final String CURRENT_USER = "CurrentUser"; private static final String AUTHORIZATION_KEY = "Authorization"; private Map<String, User> userStore = new HashMap<>(); private RestTemplate restTemplate = new RestTemplate();
@GetMapping({"/main","/"}) @ResponseBody public String main() { User user = (User) RequestContextHolder.getRequestAttributes().getAttribute(CURRENT_USER, RequestAttributes.SCOPE_SESSION); return "<html><body>hi:" + user.username + " This is Main</body></html>"; }
@GetMapping("/callback") public String callback(@RequestParam(value = "code", required = false) String code, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response) { String referer = request.getParameter("referer"); String accessToken = getAccessToken(code, buildCallbackUrl(referer)); User user = getUser(accessToken);
String uuid = UUID.randomUUID().toString(); userStore.put(uuid, user); response.addCookie(new Cookie(AUTHORIZATION_KEY, uuid)); return "redirect:" + referer; }
private String buildCallbackUrl(String referer) { return callbackUrl + "?referer=" + referer; }
private User getUser(String accessToken) { return restTemplate.getForObject(gitlabServerUrl + "/api/v4/user?access_token=" + accessToken, User.class); }
private String getAccessToken(String code, String redirectUri) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("grant_type", "authorization_code"); params.add("client_id", clientId); params.add("client_secret", clientSecret); params.add("code", code); params.add("redirect_uri", redirectUri);
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
ResponseEntity<JSONAccessTokenResponse> response = restTemplate.exchange(gitlabServerUrl + "/oauth/token", HttpMethod.POST, entity, JSONAccessTokenResponse.class); return Objects.requireNonNull(response.getBody()).access_token; }
@Configuration class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( new HandlerInterceptorAdapter() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Optional<String> authorizationKeyOp = Arrays.stream(request.getCookies()) .filter(it->it.getName().equals(AUTHORIZATION_KEY)) .map(Cookie::getValue) .findAny(); if (authorizationKeyOp.isPresent()) { RequestContextHolder.getRequestAttributes().setAttribute(CURRENT_USER, userStore.get(authorizationKeyOp.get()), RequestAttributes.SCOPE_SESSION); return super.preHandle(request, response, handler); } else { String referer = request.getRequestURL().toString(); String redirectUri = URLEncoder.encode(buildCallbackUrl(referer), "utf-8"); String gitlabAuthUrl = gitlabServerUrl + "/oauth/authorize?response_type=code&redirect_uri=" + redirectUri + "&client_id=" + clientId; logger.info("gitlabAuthUrl:{}", gitlabAuthUrl); response.sendRedirect(gitlabAuthUrl); return false; } } }) .addPathPatterns("/main", "/test"); } }
static class JSONAccessTokenResponse implements Serializable { public String access_token; }
static class User implements Serializable { public String name; public String username; } }
|