项目作者: stuffToDo

项目描述 :
Springboot HATEOAS with Spring Security & OAuth
高级语言: Java
项目地址: git://github.com/stuffToDo/AntwerpSpringRest.git
创建时间: 2020-11-02T01:18:10Z
项目社区:https://github.com/stuffToDo/AntwerpSpringRest

开源协议:

下载


AntwerpSpringRest

Overview

Basic Springboot web application to demonstrate HATEOAS and Spring Security.

Configuration

application.json

  1. spring.security.oauth2.resourceserver.jwt.issuer-uri=<issuer uri here>

OAuth

Added OAuth to the project so developers can see how it is integrated into Springboot. I used Okta to test but I didn’t leverage Okta specific libraries in the project. You can read Spring Security documentation to determine which type of OAuth token you require JWT versus Opaque. Opaque tokens provide an extra level of abstraction but require verification via the OAuth Provider which can be time consumming for GET requests. If you want to test with Opaque tokens change the line below in the SecurityConfig.

  1. .antMatchers(HttpMethod.DELETE).hasAuthority("SCOPE_antwerp_write")
  2. .anyRequest().authenticated())
  3. .oauth2ResourceServer(oauth2 -> oauth2.jwt());
  4. to
  5. // retrieve clientId/clientSecret from applications.properties
  6. .antMatchers(HttpMethod.DELETE).hasAuthority("SCOPE_antwerp_write")
  7. .anyRequest().authenticated())
  8. .oauth2ResourceServer(oauth2 -> oauth2.opaqueToken()
  9. .introspectionClientCredentials("clientId", "clientSecret"));

Integration Testing

Look at the integration test cases, this had changed quite a bit from when I last looked at it. Pay attention to the configuration, how the tests pass a fake JWT token.

  1. // Configure the JWT with scopes antwerp_read and antwerp_write
  2. private static final JwtRequestPostProcessor JWT = jwt().jwt(jwt -> jwt.claim("scope", "openid antwerp_read antwerp_write"));
  3. ...
  4. @Test
  5. void deletePortfolio() throws Exception {
  6. repo.save(new Portfolio(ID1, STOCKS));
  7. mvc.perform(delete("/portfolio/"+ID1.toString())
  8. .with(JWT)
  9. .contentType(MediaType.APPLICATION_JSON))
  10. .andExpect(status().isOk());
  11. }