80 lines
3.2 KiB
Java
80 lines
3.2 KiB
Java
package com.baeldung.security;
|
|
|
|
import com.baeldung.security.web.MySavedRequestAwareAuthenticationSuccessHandler;
|
|
import com.baeldung.security.web.RestAuthenticationEntryPoint;
|
|
import com.baeldung.web.error.CustomAccessDeniedHandler;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
import org.springframework.context.annotation.Configuration;
|
|
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.core.context.SecurityContextHolder;
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
|
|
|
|
@Configuration
|
|
@EnableWebSecurity
|
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
|
@ComponentScan("com.baeldung.security")
|
|
public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
@Autowired
|
|
private CustomAccessDeniedHandler accessDeniedHandler;
|
|
|
|
@Autowired
|
|
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
|
|
|
|
@Autowired
|
|
private MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler;
|
|
|
|
private SimpleUrlAuthenticationFailureHandler myFailureHandler = new SimpleUrlAuthenticationFailureHandler();
|
|
|
|
public SecurityJavaConfig() {
|
|
super();
|
|
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
|
|
}
|
|
|
|
@Override
|
|
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
|
auth.inMemoryAuthentication()
|
|
.withUser("admin").password(encoder().encode("adminPass")).roles("ADMIN")
|
|
.and()
|
|
.withUser("user").password(encoder().encode("userPass")).roles("USER");
|
|
}
|
|
|
|
@Override
|
|
protected void configure(final HttpSecurity http) throws Exception {
|
|
http.csrf().disable()
|
|
.authorizeRequests()
|
|
.and()
|
|
.exceptionHandling()
|
|
.accessDeniedHandler(accessDeniedHandler)
|
|
.authenticationEntryPoint(restAuthenticationEntryPoint)
|
|
.and()
|
|
.authorizeRequests()
|
|
.antMatchers("/api/csrfAttacker*").permitAll()
|
|
.antMatchers("/api/customer/**").permitAll()
|
|
.antMatchers("/api/foos/**").authenticated()
|
|
.antMatchers("/api/async/**").permitAll()
|
|
.antMatchers("/api/admin/**").hasRole("ADMIN")
|
|
.and()
|
|
.formLogin()
|
|
.successHandler(mySuccessHandler)
|
|
.failureHandler(myFailureHandler)
|
|
.and()
|
|
.httpBasic()
|
|
.and()
|
|
.logout();
|
|
}
|
|
|
|
@Bean
|
|
public PasswordEncoder encoder() {
|
|
return new BCryptPasswordEncoder();
|
|
}
|
|
|
|
} |