Skip to content

Commit aae4c26

Browse files
committed
add java configuration
1 parent 0fd7481 commit aae4c26

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎spring-security-mvc-custom/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
2121

2222
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
2323

24-
protected MySimpleUrlAuthenticationSuccessHandler() {
24+
public MySimpleUrlAuthenticationSuccessHandler() {
2525
super();
2626
}
2727

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,70 @@
11
package org.baeldung.spring;
22

3+
import org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Bean;
36
import org.springframework.context.annotation.Configuration;
4-
import org.springframework.context.annotation.ImportResource;
7+
import org.springframework.security.authentication.AuthenticationManager;
8+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
9+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
513

614
@Configuration
7-
@ImportResource({ "classpath:webSecurityConfig.xml" })
8-
public class SecSecurityConfig {
15+
//@ImportResource({ "classpath:webSecurityConfig.xml" })
16+
@EnableWebSecurity
17+
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
918

10-
11-
1219
public SecSecurityConfig() {
1320
super();
1421
}
1522

23+
@Bean("authenticationManager")
24+
@Override
25+
public AuthenticationManager authenticationManagerBean() throws Exception {
26+
return super.authenticationManagerBean();
27+
}
28+
29+
@Autowired
30+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
31+
// @formatter:off
32+
auth.inMemoryAuthentication()
33+
.withUser("user1").password("{noop}user1Pass").roles("USER")
34+
.and()
35+
.withUser("admin1").password("{noop}admin1Pass").roles("ADMIN");
36+
// @formatter:on
37+
}
38+
39+
@Override
40+
protected void configure(final HttpSecurity http) throws Exception {
41+
// @formatter:off
42+
http.authorizeRequests()
43+
.antMatchers("/anonymous*").anonymous()
44+
.antMatchers("/login*").permitAll()
45+
.anyRequest().authenticated()
46+
47+
.and()
48+
.formLogin()
49+
.loginPage("/login.html")
50+
.loginProcessingUrl("/login")
51+
.successHandler(myAuthenticationSuccessHandler())
52+
.failureUrl("/login.html?error=true")
53+
54+
.and()
55+
.logout().deleteCookies("JSESSIONID")
56+
57+
.and()
58+
.rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400)
59+
60+
.and()
61+
.csrf().disable()
62+
;
63+
// @formatter:on
64+
}
65+
66+
@Bean
67+
public AuthenticationSuccessHandler myAuthenticationSuccessHandler(){
68+
return new MySimpleUrlAuthenticationSuccessHandler();
69+
}
1670
}

0 commit comments

Comments
 (0)