11package com .example .demo .security ;
22
33import com .example .demo .auth .ApplicationUserService ;
4+ import com .example .demo .jwt .JwtConfig ;
5+ import com .example .demo .jwt .JwtTokenVerifier ;
6+ import com .example .demo .jwt .JwtUsernameAndPasswordAuthenticationFilter ;
47import org .springframework .beans .factory .annotation .Autowired ;
58import org .springframework .context .annotation .Bean ;
69import org .springframework .context .annotation .Configuration ;
1013import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1114import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1215import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
16+ import org .springframework .security .config .http .SessionCreationPolicy ;
1317import org .springframework .security .crypto .password .PasswordEncoder ;
14- import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
1518
16- import java . util . concurrent . TimeUnit ;
19+ import javax . crypto . SecretKey ;
1720
1821import static com .example .demo .security .ApplicationUserRole .*;
1922
@@ -25,43 +28,34 @@ public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
2528
2629 private final PasswordEncoder passwordEncoder ;
2730 private final ApplicationUserService applicationUserService ;
31+ private final SecretKey secretKey ;
32+ private final JwtConfig jwtConfig ;
2833
2934 @ Autowired
3035 public ApplicationSecurityConfig (PasswordEncoder passwordEncoder ,
31- ApplicationUserService applicationUserService ) {
36+ ApplicationUserService applicationUserService ,
37+ SecretKey secretKey ,
38+ JwtConfig jwtConfig ) {
3239 this .passwordEncoder = passwordEncoder ;
3340 this .applicationUserService = applicationUserService ;
41+ this .secretKey = secretKey ;
42+ this .jwtConfig = jwtConfig ;
3443 }
3544
3645 @ Override
3746 protected void configure (HttpSecurity http ) throws Exception {
3847 http
3948 .csrf ().disable ()
49+ .sessionManagement ()
50+ .sessionCreationPolicy (SessionCreationPolicy .STATELESS )
51+ .and ()
52+ .addFilter (new JwtUsernameAndPasswordAuthenticationFilter (authenticationManager (), jwtConfig , secretKey ))
53+ .addFilterAfter (new JwtTokenVerifier (secretKey , jwtConfig ),JwtUsernameAndPasswordAuthenticationFilter .class )
4054 .authorizeRequests ()
4155 .antMatchers ("/" , "index" , "/css/*" , "/js/*" ).permitAll ()
4256 .antMatchers ("/api/**" ).hasRole (STUDENT .name ())
4357 .anyRequest ()
44- .authenticated ()
45- .and ()
46- .formLogin ()
47- .loginPage ("/login" )
48- .permitAll ()
49- .defaultSuccessUrl ("/courses" , true )
50- .passwordParameter ("password" )
51- .usernameParameter ("username" )
52- .and ()
53- .rememberMe ()
54- .tokenValiditySeconds ((int ) TimeUnit .DAYS .toSeconds (21 ))
55- .key ("somethingverysecured" )
56- .rememberMeParameter ("remember-me" )
57- .and ()
58- .logout ()
59- .logoutUrl ("/logout" )
60- .logoutRequestMatcher (new AntPathRequestMatcher ("/logout" , "GET" )) // https://docs.spring.io/spring-security/site/docs/4.2.12.RELEASE/apidocs/org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html
61- .clearAuthentication (true )
62- .invalidateHttpSession (true )
63- .deleteCookies ("JSESSIONID" , "remember-me" )
64- .logoutSuccessUrl ("/login" );
58+ .authenticated ();
6559 }
6660
6761 @ Override
0 commit comments