-
Notifications
You must be signed in to change notification settings - Fork 155
/
CodeFlowExampleApplication.java
68 lines (57 loc) · 2.46 KB
/
CodeFlowExampleApplication.java
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
package com.okta.spring.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Collections;
@SpringBootApplication
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class CodeFlowExampleApplication {
public static void main(String[] args) {
SpringApplication.run(CodeFlowExampleApplication.class, args);
}
/**
* The default Spring logout behavior redirects a user back to {code}/login?logout{code}, so you will likely want
* to change that.
*/
@Configuration
static class WebConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/").permitAll()
.anyRequest().authenticated()
)
.logout().logoutSuccessUrl("/")
.and()
.oauth2Client()
.and()
.oauth2Login();
return http.build();
}
}
/**
* This example controller has endpoints for displaying the user profile info on {code}/{code} and "you have been
* logged out page" on {code}/post-logout{code}.
*/
@Controller
public class ExampleController {
@GetMapping("/")
public String home() {
return "home";
}
@GetMapping("/profile")
@PreAuthorize("hasAuthority('SCOPE_profile')")
public ModelAndView userDetails(OAuth2AuthenticationToken authentication) {
return new ModelAndView("userProfile" , Collections.singletonMap("details", authentication.getPrincipal().getAttributes()));
}
}
}