|
| 1 | +package de.dominikschadow.javasecurity; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.boot.SpringApplication; |
| 5 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 6 | +import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; |
| 7 | +import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices; |
| 8 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 9 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.core.annotation.Order; |
| 12 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 13 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 14 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 15 | +import org.springframework.security.oauth2.client.OAuth2ClientContext; |
| 16 | +import org.springframework.security.oauth2.client.OAuth2RestTemplate; |
| 17 | +import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter; |
| 18 | +import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter; |
| 19 | +import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; |
| 20 | +import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; |
| 21 | +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; |
| 22 | +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; |
| 23 | +import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; |
| 24 | +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; |
| 25 | +import org.springframework.security.web.csrf.CsrfFilter; |
| 26 | +import org.springframework.security.web.csrf.CsrfToken; |
| 27 | +import org.springframework.security.web.csrf.CsrfTokenRepository; |
| 28 | +import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; |
| 29 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 30 | +import org.springframework.web.bind.annotation.RestController; |
| 31 | +import org.springframework.web.filter.CompositeFilter; |
| 32 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 33 | +import org.springframework.web.util.WebUtils; |
| 34 | + |
| 35 | +import javax.servlet.Filter; |
| 36 | +import javax.servlet.FilterChain; |
| 37 | +import javax.servlet.ServletException; |
| 38 | +import javax.servlet.http.Cookie; |
| 39 | +import javax.servlet.http.HttpServletRequest; |
| 40 | +import javax.servlet.http.HttpServletResponse; |
| 41 | +import java.io.IOException; |
| 42 | +import java.security.Principal; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.LinkedHashMap; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Map; |
| 47 | + |
| 48 | +/** |
| 49 | + * Requires the configuration of @code{github.client.clientId} and @code{github.client.clientSecret} as runtime parameter. |
| 50 | + * This project is based on the Spring Boot and OAuth2 tutorial available at https://spring.io/guides/tutorials/spring-boot-oauth2 |
| 51 | + */ |
| 52 | +@SpringBootApplication |
| 53 | +@EnableWebSecurity |
| 54 | +@RestController |
| 55 | +@EnableOAuth2Client |
| 56 | +@EnableAuthorizationServer |
| 57 | +@Order(6) |
| 58 | +public class SsoWithGitHubApplication extends WebSecurityConfigurerAdapter { |
| 59 | + @Autowired |
| 60 | + private OAuth2ClientContext oAuth2ClientContext; |
| 61 | + |
| 62 | + public static void main(String[] args) { |
| 63 | + SpringApplication.run(SsoWithGitHubApplication.class, args); |
| 64 | + } |
| 65 | + |
| 66 | + @RequestMapping({"/user"}) |
| 67 | + public Map<String, String> user(Principal principal) { |
| 68 | + Map<String, String> map = new LinkedHashMap<>(); |
| 69 | + map.put("name", principal.getName()); |
| 70 | + return map; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void configure(HttpSecurity http) throws Exception { |
| 75 | + // @formatter:off |
| 76 | + http.antMatcher("/**") |
| 77 | + .authorizeRequests() |
| 78 | + .antMatchers("/", "/login**", "/webjars/**").permitAll() |
| 79 | + .anyRequest().authenticated() |
| 80 | + .and().exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")) |
| 81 | + .and().logout().logoutSuccessUrl("/").permitAll() |
| 82 | + .and().csrf().csrfTokenRepository(csrfTokenRepository()) |
| 83 | + .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class) |
| 84 | + .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class); |
| 85 | + // @formatter:on |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) { |
| 90 | + FilterRegistrationBean registration = new FilterRegistrationBean(); |
| 91 | + registration.setFilter(filter); |
| 92 | + registration.setOrder(-100); |
| 93 | + return registration; |
| 94 | + } |
| 95 | + |
| 96 | + @Bean |
| 97 | + @ConfigurationProperties("github") |
| 98 | + ClientResources github() { |
| 99 | + return new ClientResources(); |
| 100 | + } |
| 101 | + |
| 102 | + private Filter ssoFilter() { |
| 103 | + CompositeFilter filter = new CompositeFilter(); |
| 104 | + List<Filter> filters = new ArrayList<>(); |
| 105 | + filters.add(ssoFilter(github(), "/login/github")); |
| 106 | + filter.setFilters(filters); |
| 107 | + return filter; |
| 108 | + } |
| 109 | + |
| 110 | + private Filter ssoFilter(ClientResources client, String path) { |
| 111 | + OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = |
| 112 | + new OAuth2ClientAuthenticationProcessingFilter(path); |
| 113 | + OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client.getClient(), |
| 114 | + oAuth2ClientContext); |
| 115 | + oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate); |
| 116 | + UserInfoTokenServices tokenServices = new UserInfoTokenServices( |
| 117 | + client.getResource().getUserInfoUri(), client.getClient().getClientId()); |
| 118 | + tokenServices.setRestTemplate(oAuth2RestTemplate); |
| 119 | + oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices); |
| 120 | + return oAuth2ClientAuthenticationFilter; |
| 121 | + } |
| 122 | + |
| 123 | + private Filter csrfHeaderFilter() { |
| 124 | + return new OncePerRequestFilter() { |
| 125 | + @Override |
| 126 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
| 127 | + throws ServletException, IOException { |
| 128 | + CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); |
| 129 | + if (csrf != null) { |
| 130 | + Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN"); |
| 131 | + String token = csrf.getToken(); |
| 132 | + if (cookie == null || token != null && !token.equals(cookie.getValue())) { |
| 133 | + cookie = new Cookie("XSRF-TOKEN", token); |
| 134 | + cookie.setPath("/"); |
| 135 | + response.addCookie(cookie); |
| 136 | + } |
| 137 | + } |
| 138 | + filterChain.doFilter(request, response); |
| 139 | + } |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + private CsrfTokenRepository csrfTokenRepository() { |
| 144 | + HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); |
| 145 | + repository.setHeaderName("X-XSRF-TOKEN"); |
| 146 | + return repository; |
| 147 | + } |
| 148 | + |
| 149 | + class ClientResources { |
| 150 | + private OAuth2ProtectedResourceDetails client = new AuthorizationCodeResourceDetails(); |
| 151 | + private ResourceServerProperties resource = new ResourceServerProperties(); |
| 152 | + |
| 153 | + public OAuth2ProtectedResourceDetails getClient() { |
| 154 | + return client; |
| 155 | + } |
| 156 | + |
| 157 | + public ResourceServerProperties getResource() { |
| 158 | + return resource; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments