71 lines
2.3 KiB
Java
71 lines
2.3 KiB
Java
package com.baeldung.spring;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Profile;
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
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.web.authentication.logout.LogoutSuccessHandler;
|
|
|
|
import com.baeldung.security.CustomLogoutSuccessHandler;
|
|
|
|
@Configuration
|
|
// @ImportResource({ "classpath:channelWebSecurityConfig.xml" })
|
|
@EnableWebSecurity
|
|
@Profile("https")
|
|
public class ChannelSecSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
public ChannelSecSecurityConfig() {
|
|
super();
|
|
}
|
|
|
|
@Override
|
|
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
|
// @formatter:off
|
|
auth.inMemoryAuthentication()
|
|
.withUser("user1").password("user1Pass").roles("USER")
|
|
.and()
|
|
.withUser("user2").password("user2Pass").roles("USER");
|
|
// @formatter:on
|
|
}
|
|
|
|
@Override
|
|
protected void configure(final HttpSecurity http) throws Exception {
|
|
// @formatter:off
|
|
http
|
|
.csrf().disable()
|
|
.authorizeRequests()
|
|
.antMatchers("/anonymous*").anonymous()
|
|
.antMatchers("/login*").permitAll()
|
|
.anyRequest().authenticated()
|
|
.and()
|
|
.requiresChannel()
|
|
.antMatchers("/login*", "/perform_login").requiresSecure()
|
|
.anyRequest().requiresInsecure()
|
|
.and()
|
|
.sessionManagement()
|
|
.sessionFixation()
|
|
.none()
|
|
.and()
|
|
.formLogin()
|
|
.loginPage("/login.html")
|
|
.loginProcessingUrl("/perform_login")
|
|
.defaultSuccessUrl("/homepage.html",true)
|
|
.failureUrl("/login.html?error=true")
|
|
.and()
|
|
.logout()
|
|
.logoutUrl("/perform_logout")
|
|
.deleteCookies("JSESSIONID")
|
|
.logoutSuccessHandler(logoutSuccessHandler());
|
|
// @formatter:on
|
|
}
|
|
|
|
@Bean
|
|
public LogoutSuccessHandler logoutSuccessHandler() {
|
|
return new CustomLogoutSuccessHandler();
|
|
}
|
|
|
|
}
|