|
19 | 19 | import com.auth0.jwt.JWTVerifier;
|
20 | 20 | import com.auth0.jwt.algorithms.Algorithm;
|
21 | 21 | import com.auth0.jwt.interfaces.RSAKeyProvider;
|
| 22 | +import org.springframework.beans.BeansException; |
| 23 | +import org.springframework.beans.factory.BeanFactory; |
| 24 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 25 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
22 | 26 | import org.springframework.boot.SpringApplication;
|
23 | 27 | import org.springframework.boot.autoconfigure.SpringBootApplication;
|
24 | 28 | import org.springframework.context.annotation.Bean;
|
25 |
| -import org.springframework.http.HttpStatus; |
26 |
| -import org.springframework.security.authentication.AuthenticationManager; |
27 |
| -import org.springframework.security.authentication.AuthenticationProvider; |
28 |
| -import org.springframework.security.authentication.ProviderManager; |
29 | 29 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
30 | 30 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
31 | 31 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
| 32 | +import org.springframework.security.config.annotation.web.configurers.oauth2.resourceserver.ResourceServerConfigurer; |
32 | 33 | import org.springframework.security.oauth2.jwt.JwtDecoder;
|
33 |
| -import org.springframework.security.oauth2.resourceserver.access.expression.OAuth2ResourceServerExpressions; |
34 |
| -import org.springframework.security.oauth2.resourceserver.access.expression.OAuth2Expressions; |
35 |
| -import org.springframework.security.oauth2.resourceserver.authentication.JwtAccessTokenAuthenticationProvider; |
36 |
| -import org.springframework.security.oauth2.resourceserver.authentication.JwtAccessTokenVerifier; |
37 |
| -import org.springframework.security.oauth2.resourceserver.web.BearerTokenAuthenticationFilter; |
38 |
| -import org.springframework.security.web.AuthenticationEntryPoint; |
39 |
| -import org.springframework.security.web.authentication.HttpStatusEntryPoint; |
40 |
| -import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; |
41 | 34 |
|
42 | 35 | import java.io.InputStream;
|
43 |
| -import java.util.Arrays; |
44 | 36 |
|
45 | 37 | @SpringBootApplication
|
46 |
| -public class MessagesApplication { |
| 38 | +public class MessagesApplication implements BeanFactoryAware { |
| 39 | + |
| 40 | + private ConfigurableBeanFactory beanFactory; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 44 | + if ( beanFactory instanceof ConfigurableBeanFactory ) { |
| 45 | + this.beanFactory = (ConfigurableBeanFactory) beanFactory; |
| 46 | + } |
| 47 | + } |
47 | 48 |
|
48 | 49 | @EnableGlobalMethodSecurity(prePostEnabled = true)
|
49 | 50 | class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
50 | 51 | @Override
|
51 | 52 | protected void configure(HttpSecurity http) throws Exception {
|
52 |
| - http |
53 |
| - .addFilterAfter( |
54 |
| - oauthResourceAuthenticationFilter(), |
55 |
| - BasicAuthenticationFilter.class) |
56 |
| - .exceptionHandling() |
57 |
| - .authenticationEntryPoint(restAuthenticationEntryPoint()).and() |
58 |
| - .authorizeRequests() |
59 |
| - .anyRequest().authenticated().and() |
60 |
| - .csrf().disable(); |
61 |
| - } |
62 |
| - } |
63 |
| - |
64 |
| - |
65 |
| - |
66 |
| - @Bean |
67 |
| - public OAuth2Expressions oauth2() { |
68 |
| - return new OAuth2ResourceServerExpressions(); |
69 |
| - } |
70 | 53 |
|
71 |
| - // @Bean -- We don't want this to get wired by Spring Boot as a servlet-level filter |
72 |
| - // Is there a more clever way to do this? |
73 |
| - BearerTokenAuthenticationFilter oauthResourceAuthenticationFilter() { |
74 |
| - BearerTokenAuthenticationFilter filter = |
75 |
| - new BearerTokenAuthenticationFilter(authenticationManager()); |
| 54 | + resourceServer() |
| 55 | + .jwt(jwtDecoder()) |
76 | 56 |
|
77 |
| - return filter; |
78 |
| - } |
79 |
| - |
80 |
| - @Bean |
81 |
| - AuthenticationManager authenticationManager() { |
82 |
| - return new ProviderManager( |
83 |
| - Arrays.asList(oauthResourceAuthenticationProvider()) |
84 |
| - ); |
85 |
| - } |
| 57 | + .and().apply(http); |
| 58 | + } |
86 | 59 |
|
87 |
| - @Bean |
88 |
| - AuthenticationProvider oauthResourceAuthenticationProvider() { |
89 |
| - JwtAccessTokenAuthenticationProvider provider = |
90 |
| - new JwtAccessTokenAuthenticationProvider(jwtDecoder(), new JwtAccessTokenVerifier()); |
| 60 | + protected ResourceServerConfigurer resourceServer() { |
| 61 | + return new ResourceServerConfigurer(MessagesApplication.this.beanFactory); |
| 62 | + } |
91 | 63 |
|
92 |
| - return provider; |
93 | 64 | }
|
94 | 65 |
|
95 | 66 | @Bean
|
96 | 67 | JwtDecoder jwtDecoder() {
|
97 | 68 | InputStream is = this.getClass().getClassLoader().getResourceAsStream("id_rsa.pub");
|
98 | 69 | RSAKeyProvider provider = new PemParsingPublicKeyOnlyRSAKeyProvider(is);
|
99 | 70 | JWTVerifier verifier = JWT.require(Algorithm.RSA256(provider)).withIssuer("rob").build();
|
100 |
| - return new Auth0JwtDecoderJwkSupport(verifier); |
101 |
| - } |
102 |
| - |
103 |
| - @Bean |
104 |
| - AuthenticationEntryPoint restAuthenticationEntryPoint() { |
105 |
| - return new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED); |
| 71 | + return new Auth0JwtDecoder(verifier); |
106 | 72 | }
|
107 | 73 |
|
108 | 74 | public static void main(String[] args) {
|
|
0 commit comments