DataJPA/src/main/kotlin/com/chantha/jdbc/config/WebConfig.java

60 lines
2.3 KiB
Java
Raw Normal View History

2020-05-18 14:51:58 +07:00
package com.chantha.jdbc.config;
2020-05-19 16:51:38 +07:00
import com.chantha.jdbc.utils.jwt.JwtRequestFilter;
import org.springframework.beans.factory.annotation.Autowired;
2020-05-19 15:30:16 +07:00
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2020-05-18 17:11:01 +07:00
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2020-05-18 17:11:01 +07:00
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
2020-05-19 15:30:16 +07:00
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
2020-05-19 16:51:38 +07:00
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
2020-05-18 17:11:01 +07:00
@Configuration
@EnableWebSecurity
2020-05-18 17:11:01 +07:00
public class WebConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
2020-05-19 16:51:38 +07:00
@Autowired
private JwtRequestFilter jwtRequestFilter;
2020-05-19 15:30:16 +07:00
@Autowired
public WebConfig(UserDetailsService userDetailsService){
this.userDetailsService=userDetailsService;
}
2020-05-18 17:11:01 +07:00
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
2020-05-19 15:30:16 +07:00
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
2020-05-18 17:11:01 +07:00
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin();
http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
http.authorizeRequests()
2020-05-19 15:30:16 +07:00
.antMatchers("/register").permitAll()
.antMatchers("/**").hasAnyRole("ADMIN");
http.csrf().disable();
2020-05-19 16:51:38 +07:00
http.addFilterBefore(jwtRequestFilter,UsernamePasswordAuthenticationFilter.class);
2020-05-18 17:11:01 +07:00
}
2020-05-19 15:30:16 +07:00
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
2020-05-18 17:11:01 +07:00
2020-05-18 14:51:58 +07:00
}