|
| 1 | +package com.example.demo.auth; |
| 2 | + |
| 3 | +import com.example.demo.auth.jwt.JwtAuthenticationFilter; |
| 4 | +import com.example.demo.auth.jwt.JwtFormLoginSuccessHandler; |
| 5 | +import com.example.demo.user.UserService; |
| 6 | +import lombok.AllArgsConstructor; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.annotation.Bean; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.security.access.hierarchicalroles.RoleHierarchy; |
| 12 | +import org.springframework.security.authentication.AuthenticationManager; |
| 13 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 14 | +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; |
| 15 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 16 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 17 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 18 | +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
| 19 | +import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; |
| 20 | +import org.springframework.security.config.http.SessionCreationPolicy; |
| 21 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 22 | +import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler; |
| 23 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 24 | +import org.springframework.security.web.context.RequestAttributeSecurityContextRepository; |
| 25 | + |
| 26 | +@AllArgsConstructor |
| 27 | +@EnableMethodSecurity |
| 28 | +@EnableWebSecurity |
| 29 | +@Configuration |
| 30 | +public class SecurityConfiguration extends WebSecurityConfigurerAdapter { |
| 31 | + |
| 32 | + private final UserService userService; |
| 33 | + private final PasswordEncoder passwordEncoder; |
| 34 | + private final RoleHierarchy roleHierarchy; |
| 35 | + |
| 36 | + @Bean |
| 37 | + @Override |
| 38 | + public AuthenticationManager authenticationManagerBean() throws Exception { |
| 39 | + return super.authenticationManagerBean(); |
| 40 | + } |
| 41 | + |
| 42 | + @Autowired |
| 43 | + @Override |
| 44 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 45 | + auth.userDetailsService(userService).passwordEncoder(passwordEncoder); |
| 46 | + } |
| 47 | + |
| 48 | + @Autowired |
| 49 | + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 50 | + auth.userDetailsService(userService).passwordEncoder(passwordEncoder); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void configure(HttpSecurity http) throws Exception { |
| 55 | + http.csrf(AbstractHttpConfigurer::disable); |
| 56 | + http.headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin)); |
| 57 | + http.httpBasic(AbstractHttpConfigurer::disable); |
| 58 | + |
| 59 | + http.formLogin() |
| 60 | + .successHandler(new JwtFormLoginSuccessHandler()) |
| 61 | + .and() |
| 62 | + .logout().logoutUrl("/logout") |
| 63 | + .permitAll().deleteCookies(HttpHeaders.AUTHORIZATION); |
| 64 | + |
| 65 | + http.authorizeRequests() |
| 66 | + .antMatchers("/h2-console", "/h2-console/**").permitAll() |
| 67 | + .antMatchers("/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**").permitAll() |
| 68 | + .antMatchers("/login", "/error").permitAll() |
| 69 | + .anyRequest().authenticated() |
| 70 | + .expressionHandler(webSecurityExpressionHandler()) |
| 71 | + .and() |
| 72 | + .addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); |
| 73 | + |
| 74 | + http.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)); |
| 75 | + http.securityContext(sc -> sc.securityContextRepository(new RequestAttributeSecurityContextRepository())); |
| 76 | + } |
| 77 | + |
| 78 | + @Bean |
| 79 | + public DefaultWebSecurityExpressionHandler webSecurityExpressionHandler() { |
| 80 | + DefaultWebSecurityExpressionHandler webSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler(); |
| 81 | + webSecurityExpressionHandler.setRoleHierarchy(roleHierarchy); |
| 82 | + return webSecurityExpressionHandler; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments