Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Spring Boot 2.1 #99

Merged
merged 12 commits into from
Dec 14, 2018
Merged

Update to Spring Boot 2.1 #99

merged 12 commits into from
Dec 14, 2018

Conversation

bdemers
Copy link
Contributor

@bdemers bdemers commented Nov 6, 2018

Which pulls in Spring Security 5.1

This is a complete rewrite as the Spring Security OAuth modules have changed (so looking at the diffs will look odd, you might be better off treating it as new work and looking at the files)

Fixes: #96

@mraible
Copy link
Contributor

mraible commented Nov 6, 2018

Is this backward compatible with Spring Boot 2.0? FWIW, I updated Build a Basic CRUD App with Angular 7.0 and Spring Boot 2.1 to use Spring Boot 2.1 with the 0.6.1 version, and it works fine.

I also updated the following posts, but they use Spring Security 5.1 directly without our starter.

@bdemers
Copy link
Contributor Author

bdemers commented Nov 6, 2018

Possibly, with the exception of the resource server. I still need to make a final pass through this and the samples repo, but I'll try 2.0 and Spring Sec 5.0 to see where we stand

@bdemers bdemers requested review from mraible and dogeared November 7, 2018 20:47
@bdemers
Copy link
Contributor Author

bdemers commented Nov 7, 2018

@mraible Just tried with 2.0.5, no luck, there was an issue loading the ResourceServer auto config, which could be worked around by adding something like:

@ConditionalOnClass({JwtAuthenticationToken.class, OAuth2ResourceServerProperties.class})

But then I ran into another issue with thymeleaf-security, so i'd say 2.1 is the min.

@mraible
Copy link
Contributor

mraible commented Nov 7, 2018 via email

@bdemers
Copy link
Contributor Author

bdemers commented Nov 7, 2018

Agreed

@bdemers bdemers mentioned this pull request Nov 7, 2018
5 tasks
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@okta okta deleted a comment Nov 9, 2018
@bdemers
Copy link
Contributor Author

bdemers commented Nov 12, 2018

@okta okta deleted a comment Nov 14, 2018
@mraible mraible changed the title Update to Spring boot 2.1 Update to Spring Boot 2.1 Nov 18, 2018
@mraible
Copy link
Contributor

mraible commented Nov 18, 2018

@bdemers I tried upgrading three of my Spring Boot 2.1 examples to use this version. Here's what I found (links are to PRs with the code changes):

screen shot 2018-11-18 at 12 44 56 pm

screen shot 2018-11-18 at 12 52 54 pm

Copy link
Contributor

@mraible mraible left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested both the React CRUD example (uses auth code flow) and the Angular CRUD example (uses implicit) and they both work with the latest code. 👍

@okta okta deleted a comment Dec 2, 2018
@ghost
Copy link

ghost commented Dec 11, 2018

I'm updating my apps to boot 2.1.1 and this version of the starter and there appears to be a change that's breaking our functionality. With the 0.6 starter and boot 2.0, the JWT token that gets generated after successful authentication (auth code flow) has the "aud" (audience) property set to "api://default" which is the default audience configured for my default authorization server in Okta.

With this new starter (1.0.0-SNAPSHOT) and boot 2.1.1, the "aud" property in the token is now set to the "client id" of the application instead, and breaks a few things for me I can elaborate on. But why is the behavior different different? Can I configure it to behave the way it was? Basically I use the same token to call into other resource servers on behalf of the user and now those servers reject the token because the "audience" no longer matches the expected "api://default". I am guessing this has more to do with spring boot and spring security than the okta starter itself but it's a kind of a breaking change anyway for me. Any thoughts?

@bdemers
Copy link
Contributor Author

bdemers commented Dec 11, 2018

Hey @cah-calixtomelean!

It sounds like your code might be grabbing an id_token instead of an access token. Can you share the the snippet of code you are using to get the token?

@ghost
Copy link

ghost commented Dec 12, 2018

Hey @cah-calixtomelean!

It sounds like your code might be grabbing an id_token instead of an access token. Can you share the the snippet of code you are using to get the token?

@bdemers you are absolutely correct, I was doing that (used to work); that said, I changed the code to the following yesterday and it appeared to "fix it" though I'm not sure that's the best way, what do you think?

@Autowired
OAuth2AuthorizedClientService clientService;

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
OAuth2AuthenticationToken oauthToken = (OAuth2AuthenticationToken) authentication;
String clientRegistrationId = oauthToken.getAuthorizedClientRegistrationId();
OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(clientRegistrationId, oauthToken.getName());
String accessToken = client.getAccessToken().getTokenValue();

@bdemers
Copy link
Contributor Author

bdemers commented Dec 12, 2018

@cah-calixtomelean That looks about right to me, it is similar to what Matt and Joe wrote about here:
https://developer.okta.com/blog/2017/12/18/spring-security-5-oidc

It does seem overly verbose. @mraible thoughts/ideas?

A side note, this is a breaking change release (as is the new Spring Security OAuth project)

@mraible
Copy link
Contributor

mraible commented Dec 12, 2018

I believe there are two ways to get an access token, but I haven't tried either, so I'm not sure.

OAuth2RestTemplate.getOAuth2ClientContext().getAccessToken()

Or if you're using Spring Boot 2.1:

public ResponseEntity<?> method(@AuthenticationPrincipal(expression = "accessToken") OAuth2AccessToken accessToken) { ... }

@ghost
Copy link

ghost commented Dec 12, 2018

Or if you're using Spring Boot 2.1:

public ResponseEntity<?> method(@AuthenticationPrincipal(expression = "accessToken") OAuth2AccessToken accessToken) { ... }

EL1008E: Property or field 'accessToken' cannot be found on object of type 'org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser' - maybe not public or not valid?

that's the thing, DefaultOidcUser has an id_token

@mraible
Copy link
Contributor

mraible commented Dec 12, 2018

Sorry, I'm wrong. It was just a guess. Maybe @rwinch knows a simpler way to get an access token in Spring Boot 2.1?

@ghost
Copy link

ghost commented Dec 12, 2018

@bdemers any plans to update this example?
https://developer.okta.com/blog/2018/04/02/client-creds-with-spring-boot

that method security stuff (@EnableGlobalMethodSecurity) and the @PreAuthorize("#oauth2.hasScope('custom_mod')") annotation mechanism is not working for me with this version of the starter and springboot. It works fine in spring boot 2 and the 0.6 starter. I keep getting 403 when hitting the method.

EDIT: never mind. changing @PreAuthorize("#oauth2.hasScope('custom_mod')") to @PreAuthorize("hasAuthority('SCOPE_custom_mod')") fixed it.

@rwinch
Copy link

rwinch commented Dec 12, 2018

You can get get the access token using @RegisteredOAuth2AuthorizedClient. I'd also take a look at the WebClient integration and the oauth2webclient sample.

@bdemers
Copy link
Contributor Author

bdemers commented Dec 12, 2018

@cah-calixtomelean yup, we will get our docs and blog posts updated soon!

The @PreAuthorize("#oauth2.hasScope('custom_mod')") annotation becomes @PreAuthorize("hasAuthority('SCOPE_custom_mod')") in Spring Security 5+

@bdemers bdemers merged commit e78d185 into master Dec 14, 2018
@bdemers bdemers deleted the spring-boot-2.1 branch December 14, 2018 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants