일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- reactive
- 웹 스터디
- 웹 커리큘럼
- 웹앱
- 서버운영
- spring reactive
- Spring Batch
- ipTIME
- Spring Framework
- reactor
- 공유기 서버
- reactor core
- Today
- Total
Hello World
Authentication against a RESTful Service with Spring Security 본문
Authentication against a RESTful Service with Spring Security
EnterKey 2016. 1. 10. 12:561. Overview
This article is focused on how to authenticate against a secure REST API that provides security services – mainly, a RESTful User Account and Authentication Service.
2. The Goal
First, let’s go over the actors – the typical Spring Security enabled application needs to authenticate against something – that something can be a database, LDAP or it can be a REST service. The database is the most common scenario; however, a RESTful UAA (User Account and Authentication) Service can work just as well.
For the purpose of this article, the REST UAA Service will expose a single GET operation on /authentication, which will return the Principal information required by Spring Security to perform the full authentication process.
3. The Client
Typically, a simple Spring Security enabled application would use a simple user service as the authentication source:
This would implement the org.springframework.security.core.userdetails.UserDetailsService and would return the Principal based on a provided username:
When a Client authenticates against the RESTful UAA Service, working only with the username will no longer be enough– the client now needs the full credentials – both username and password – when it’s sending the authentication request to the service. This makes perfect sense, as the service itself is secured, so the request itself needs to contain the authentication credentials in order to be handled properly.
From the point of view or Spring Security, this cannot be done from within loadUserByUsername because the password is no longer available at that point – we need to take control of the authentication process sooner.
We can do this by providing the full authentication provider to Spring Security:
Overriding the entire authentication provider gives us a lot more freedom to perform custom retrieval of the Principal from the Service, but it does come with a fair bit of complexity. The standard authentication provider –DaoAuthenticationProvider – has most of what we need, so a good approach would be to simply extend it and modify only what is necessary.
Unfortunately this is not possible, as retrieveUser – the method we would be interested in extending – is final. This is somewhat unintuitive (there is a JIRA discussing the issue) – it looks like the design intention here is simply to provide an alternative implementation which is not ideal, but not a major problem either – our RestAuthenticationProvider copy-pastes most of the implementation of DaoAuthenticationProvider and rewrites what it needs to – the retrieval of the principal from the service:
Let’s start from the beginning – the HTTP communication with the REST Service – this is handled by theauthenticationApi – a simple API providing the authenticate operation for the actual service. The operation itself can be implemented with any library capable of HTTP – in this case, the implementation is using RestTemplate:
A FactoryBean can be used to set up the RestTemplate in the context.
Next, if the authentication request resulted in a HTTP 401 Unauthorized, most likely because of incorrect credentials from the client, a principal with wrong credentials is returned so that the Spring Security authentication process can refuse them:
Finally, the Spring Security Principal needs some authorities – the privileges which that particular principal will have and use locally after the authentication process. The /authenticate operation had retrieved a full principal, including privileges, so these need to be extracted from the result of the request and transformed into GrantedAuthority objects, as required by Spring Security.
The details of how these privileges are stored is irrelevant here – they could be stored as simple Strings or as a complex Role-Privilege structure – but regardless of the details, we only need to use their names to construct the GrantedAuthoritiyobjects. After the final Spring Security principal is created, it is returned back to the standard authentication process:
4. Testing the Authentication Service
Writing an integration test that consumes the authentiction REST service on the happy-path is straightforward enough:
Following this simple test, more complex integration tests can be implemented as well – however this is outside of the scope of this post.
5. Conclusion
This article explained how to authenticate against a REST Service instead of doing so against a local system such as a database. For a full implementation of a secure RESTful service which can be used as an authentication provider, check out the github project.
출처: http://www.javacodegeeks.com/2012/12/authentication-against-a-restful-service-with-spring-security.html?utm_content=bufferd05e2&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer
'Spring > Boot(4.x)' 카테고리의 다른 글
개발용 PC와 운영체제 인코딩 설정 따른 톰캣 WAR 파일 한글 문제 해결 (0) | 2016.01.10 |
---|---|
[펌]spring security에서 sha256에서 bcrypt로 암호화 방식을 전환하는 방법 (0) | 2016.01.10 |
Spring-loadded로 WAS 재기동 없이 개발하기 (0) | 2016.01.10 |
Spring-loaded를 IntelliJ 와 Gradle를 사용하여 설정 하기 (0) | 2016.01.10 |
[펌]Spring Cloud AWS를 이용한 S3 연동 (0) | 2016.01.10 |