14
14
import org .springframework .security .core .userdetails .UserDetailsService ;
15
15
import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
16
16
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
17
+ import org .springframework .web .filter .CorsFilter ;
17
18
18
19
@ Configuration
19
20
@ EnableWebSecurity
20
21
@ Order (1 )
21
22
public class SecurityConfig extends WebSecurityConfigurerAdapter {
22
23
23
- final private UserService userService ;
24
+ private final UserService userService ;
24
25
25
- final private TokenAuthenticationService tokenAuthenticationService ;
26
+ private final TokenAuthenticationService tokenAuthenticationService ;
27
+ private final CorsFilter corsFilter ;
26
28
27
29
@ Autowired
28
- public SecurityConfig (UserService userService , TokenAuthenticationService tokenAuthenticationService ) {
30
+ public SecurityConfig (UserService userService ,
31
+ TokenAuthenticationService tokenAuthenticationService ,
32
+ CorsFilter corsFilter ) {
29
33
super (true );
30
34
this .userService = userService ;
31
35
this .tokenAuthenticationService = tokenAuthenticationService ;
36
+ this .corsFilter = corsFilter ;
32
37
}
33
38
34
39
@ Override
@@ -47,11 +52,15 @@ protected void configure(HttpSecurity http) throws Exception {
47
52
.antMatchers (HttpMethod .GET , "/api/users" ).hasRole ("USER" )
48
53
.antMatchers (HttpMethod .GET , "/api/users/me" ).hasRole ("USER" )
49
54
.antMatchers (HttpMethod .GET , "/api/users/me/microposts" ).hasRole ("USER" )
50
- .antMatchers ("/api/microposts/**" ).hasRole ("USER" )
51
- .antMatchers ("/api/relationships/**" ).hasRole ("USER" )
52
- .antMatchers ("/api/feed" ).hasRole ("USER" )
55
+ .antMatchers (HttpMethod .POST , "/api/microposts/**" ).hasRole ("USER" )
56
+ .antMatchers (HttpMethod .DELETE , "/api/microposts/**" ).hasRole ("USER" )
57
+ .antMatchers (HttpMethod .POST , "/api/relationships/**" ).hasRole ("USER" )
58
+ .antMatchers (HttpMethod .DELETE , "/api/relationships/**" ).hasRole ("USER" )
59
+ .antMatchers (HttpMethod .GET , "/api/feed" ).hasRole ("USER" )
60
+ .antMatchers (HttpMethod .OPTIONS , "/api/**" ).permitAll ()
53
61
;
54
62
63
+ http .addFilterBefore (corsFilter , UsernamePasswordAuthenticationFilter .class );
55
64
http .addFilterBefore (
56
65
new StatelessLoginFilter (
57
66
"/api/login" ,
0 commit comments