Skip to content

Commit 3bb91d5

Browse files
author
eugenp
committed
new java config
1 parent 2245240 commit 3bb91d5

3 files changed

Lines changed: 81 additions & 16 deletions

File tree

‎spring-security-rest/src/main/java/org/baeldung/spring/SecSecurityConfig.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.baeldung.spring;
2+
3+
import org.baeldung.security.MySavedRequestAwareAuthenticationSuccessHandler;
4+
import org.baeldung.security.RestAuthenticationEntryPoint;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
10+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
11+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
12+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
13+
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
14+
15+
@Configuration
16+
@EnableWebSecurity
17+
@ComponentScan("org.baeldung.security")
18+
public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
19+
20+
@Autowired
21+
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
22+
23+
@Autowired
24+
private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler;
25+
26+
public SecurityJavaConfig() {
27+
super();
28+
}
29+
30+
//
31+
32+
@Override
33+
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
34+
auth.inMemoryAuthentication().withUser("temporary").password("temporary").roles("ADMIN").and().withUser("user").password("userPass").roles("USER");
35+
}
36+
37+
@Override
38+
protected void configure(final HttpSecurity http) throws Exception { // @formatter:off
39+
http
40+
.csrf().disable()
41+
.exceptionHandling()
42+
.authenticationEntryPoint(restAuthenticationEntryPoint)
43+
.and()
44+
.authorizeRequests()
45+
.antMatchers("/api/foos").authenticated()
46+
.and()
47+
.formLogin()
48+
.loginProcessingUrl("/j_spring_security_check")
49+
.usernameParameter("j_username")
50+
.passwordParameter("j_password")
51+
.successHandler(authenticationSuccessHandler)
52+
.failureHandler(new SimpleUrlAuthenticationFailureHandler())
53+
.and()
54+
.logout();
55+
} // @formatter:on
56+
57+
@Bean
58+
public MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler(){
59+
return new MySavedRequestAwareAuthenticationSuccessHandler();
60+
}
61+
62+
@Bean
63+
public SimpleUrlAuthenticationFailureHandler myFailureHandler(){
64+
return new SimpleUrlAuthenticationFailureHandler();
65+
}
66+
67+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.spring;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
5+
// @Configuration
6+
// @ImportResource({ "classpath:webSecurityConfig.xml" })
7+
@ComponentScan("org.baeldung.security")
8+
public class SecurityXmlConfig {
9+
10+
public SecurityXmlConfig() {
11+
super();
12+
}
13+
14+
}

0 commit comments

Comments
 (0)