67 lines
2.7 KiB
Java
Raw Normal View History

2019-10-15 23:08:57 +07:00
package com.bezkoder.springjwt.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import com.bezkoder.springjwt.security.jwt.AuthEntryPointJwt;
import com.bezkoder.springjwt.security.jwt.AuthTokenFilter;
import com.bezkoder.springjwt.security.services.UserDetailsServiceImpl;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
2021-12-11 12:13:08 +07:00
// securedEnabled = true,
// jsr250Enabled = true,
prePostEnabled = true)
2019-10-15 23:08:57 +07:00
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
2021-12-11 12:13:08 +07:00
@Autowired
UserDetailsServiceImpl userDetailsService;
2019-10-15 23:08:57 +07:00
2021-12-11 12:13:08 +07:00
@Autowired
private AuthEntryPointJwt unauthorizedHandler;
2019-10-15 23:08:57 +07:00
2021-12-11 12:13:08 +07:00
@Bean
public AuthTokenFilter authenticationJwtTokenFilter() {
return new AuthTokenFilter();
}
2019-10-15 23:08:57 +07:00
2021-12-11 12:13:08 +07:00
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
2019-10-15 23:08:57 +07:00
2021-12-11 12:13:08 +07:00
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
2019-10-15 23:08:57 +07:00
2021-12-11 12:13:08 +07:00
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
2019-10-15 23:08:57 +07:00
2021-12-11 12:13:08 +07:00
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests().antMatchers("/api/auth/**").permitAll()
.antMatchers("/api/test/**").permitAll()
.anyRequest().authenticated();
2019-10-15 23:08:57 +07:00
2021-12-11 12:13:08 +07:00
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
2019-10-15 23:08:57 +07:00
}