다양한 방법을 강구 했으나, 최종 낙찰은 그냥 무식하게 Filter 하나 낑겨 넣고, 일단 OAuth2 쪽에 인증 확인하는 방법으로 구현했습니다. OAuth2 Provider 도 만들어야 해서 도무지 시간이 나질 않네요. 다 공개하긴 X팔리고, 걍 몇가지 코드만 적어 놓겠습니다.
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
// bypassOAuth 파라메터가 true 일 경우, 해당 필터는 사용되지 않습니다.
// 독립 로그인 페이지를 직접 접근할 경우를 위해 마련함.
if (request.getParameter("bypassOAuth") != null
|| request.getSession().getAttribute("share.bypassOAuth") != null) {
request.getSession().setAttribute("share.bypassOAuth", true);
chain.doFilter(servletRequest, servletResponse);
return;
}
// 이미 인증이 되어 있다면, 패스
boolean isAlfAuthed = AuthenticationUtil.isAuthenticated(request);
System.out.println("isAlfAuthed : " + isAlfAuthed);
if (isAlfAuthed) {
chain.doFilter(request, response);
return;
}
String username = this.doOAuthAuthentication(request, response);
System.out.println("username : " + username);
if (username != null) {
UserFactory userFactory = (UserFactory)getApplicationContext().getBean("user.factory");
boolean authenticated = userFactory.authenticate(request, username, USER_PASSWORD);
System.out.println("authenticated : " + authenticated);
if (authenticated) {
AuthenticationUtil.login(request, response, username);
}
}
chain.doFilter(servletRequest, servletResponse);
}
protected String doOAuthAuthentication(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//요청값에 이미 code 가 있는지 확인
String authCode = request.getParameter("code");
//Token requestToken = (Token) request.getSession().getAttribute(ATTR_OAUTH_REQUEST_TOKEN);
if (authCode == null) {
try {
this.processNoRequestToken(request, response);
return null;
} catch (Exception e) {
e.printStackTrace();
logger.debug("Authentication failed: " + e.getMessage());
}
} else {
try {
return this.processRequestToken(request, response, authCode);
} catch (Exception e) {
e.printStackTrace();
logger.debug("Authentication failed: " + e.getMessage());
}
}
return null;
}
더불어, Spring-Secruity-OAuth2 의 경우, /auth /token 등 URL에 form-based client-id/secret 인증을 수행합니다. 인증 안하면, URL 접근자체가 안되서, 좀 난감했군요. 아래처럼, BASE64 로 인코딩해서 해더에 넣어주면 되겠습니다.
String auth = config.getApiKey() + ":" + config.getApiSecret();
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
request.addHeader(HttpHeaders.AUTHORIZATION, authHeader);
단순한, 필터말고 더 낳은 방법이 분명히 있을거라 생각됩니다만,, 좀 아시면 댓글로.. 굽신굽신..
'알프레스코' 카테고리의 다른 글
MT(multi-tenancy) 환경에서의 bootstrap 문제. (0) | 2015.01.12 |
---|---|
알프레스코의 티켓이 이유없이 없어지거나, 401 오류가 자주 발생한다면? (0) | 2014.10.22 |
알프레스코 로그인 Authentication Customizing (0) | 2014.07.29 |
알프레스코 클러스터링 구성 정리 (0) | 2014.07.24 |
알프레스코 ORACLE 파티셔닝 테스트 (0) | 2014.07.24 |