daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Jul 19th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @SpringBootApplication
  2. @EnableOAuth2Client
  3. @RestController
  4. @EnableAuthorizationServer
  5.  
  6. public class Oauthtutorial1Application extends WebSecurityConfigurerAdapter {
  7.  
  8. @RequestMapping("/user")
  9. public Principal user(Principal principal) {
  10. return principal;
  11. }
  12.  
  13. public static void main(String[] args) {
  14.   SpringApplication.run(Oauthtutorial1Application.class, args);
  15. }
  16.  
  17. @Autowired
  18. OAuth2ClientContext oauth2ClientContext;
  19.  
  20. @Bean
  21. public FilterRegistrationBean oauth2ClientFilterRegistration(
  22.     OAuth2ClientContextFilter filter) {
  23. FilterRegistrationBean registration = new FilterRegistrationBean();
  24. registration.setFilter(filter);
  25. registration.setOrder(-100);
  26. return registration;
  27. }
  28. @Override
  29. protected void configure(HttpSecurity http) throws Exception {
  30. http
  31.         .antMatcher("/**")
  32.         .authorizeRequests()
  33.         .antMatchers( "/","/login**", "/webjars/**", "/error**")
  34.         .permitAll()
  35.         .anyRequest()
  36.         .authenticated()
  37.         .and().exceptionHandling()
  38.         .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
  39. .and().logout().logoutSuccessUrl("/").permitAll()
  40. .and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  41.         .and().addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
  42. }
  43.  
  44. private Filter ssoFilter() {
  45. CompositeFilter filter = new CompositeFilter();
  46. List<Filter> filters = new ArrayList<>();
  47. filters.add(ssoFilter(facebook(), "/login/facebook"));
  48. filters.add(ssoFilter(github(), "/login/github"));
  49. filter.setFilters(filters);
  50. return filter;
  51. }
  52.  
  53. private Filter ssoFilter(ClientResources client, String path) {
  54. OAuth2ClientAuthenticationProcessingFilter filter = new             OAuth2ClientAuthenticationProcessingFilter(path);
  55. OAuth2RestTemplate template = new OAuth2RestTemplate(client.getClient(),    oauth2ClientContext);
  56. filter.setRestTemplate(template);
  57. UserInfoTokenServices tokenServices = new UserInfoTokenServices(
  58.         client.getResource().getUserInfoUri(), client.getClient().getClientId());
  59. tokenServices.setRestTemplate(template);
  60. filter.setTokenServices(tokenServices);
  61. return filter;
  62. }
  63. class ClientResources {
  64. @NestedConfigurationProperty
  65. private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails();
  66. @NestedConfigurationProperty
  67. private ResourceServerProperties resource = new ResourceServerProperties();
  68.  
  69. public AuthorizationCodeResourceDetails getClient() {
  70.     return client;
  71. }
  72.  
  73. public ResourceServerProperties getResource() {
  74.     return resource;
  75. }
  76. }
  77.  
  78. @Bean
  79. @ConfigurationProperties("github")
  80. public ClientResources github() {
  81. return new ClientResources();
  82. }
  83.  
  84. @Bean
  85. @ConfigurationProperties("facebook")
  86. public ClientResources facebook() {
  87. return new ClientResources();
  88. }
  89. }
  90.    
  91. <build>
  92.     <plugins>
  93.         <plugin>
  94.             <groupId>org.springframework.boot</groupId>
  95.             <artifactId>spring-boot-maven-plugin</artifactId>
  96.         </plugin>
  97.     </plugins>
  98. </build>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top