Skip to content

Commit 5a8ee24

Browse files
danielguedesbmaibin
authored andcommitted
BAEL-1914 refactor (eugenp#4749)
* Server Sent Events example using Spring Webflux and React * spring security custom AuthenticationFailureHandler * refactor * moved SSE to branch * remove pom properties * moved AuthenticationFailureHandler example to spring-security-mvc-login
1 parent 1f03524 commit 5a8ee24

8 files changed

Lines changed: 119 additions & 278 deletions

File tree

‎spring-boot-security/src/main/java/com/baeldung/springbootsecurity/form_login/SpringBootSecurityApplication.java‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎spring-boot-security/src/main/java/com/baeldung/springbootsecurity/form_login/configuration/SecurityConfiguration.java‎

Lines changed: 0 additions & 67 deletions
This file was deleted.

‎spring-boot-security/src/test/java/com/baeldung/springbootsecurity/form_login/FormLoginIntegrationTest.java‎

Lines changed: 0 additions & 87 deletions
This file was deleted.

‎spring-boot-security/src/test/java/com/baeldung/springbootsecurity/form_login/FormLoginUnitTest.java‎

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.security;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.security.core.AuthenticationException;
5+
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
import java.util.Calendar;
12+
13+
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
14+
15+
@Override
16+
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
17+
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
18+
19+
String jsonPayload = "{\"message\" : \"%s\", \"timestamp\" : \"%s\" }";
20+
httpServletResponse.getOutputStream().println(String.format(jsonPayload, e.getMessage(), Calendar.getInstance().getTime()));
21+
}
22+
}
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.baeldung.spring;
22

33
import org.baeldung.security.CustomAccessDeniedHandler;
4+
import org.baeldung.security.CustomAuthenticationFailureHandler;
45
import org.baeldung.security.CustomLogoutSuccessHandler;
56
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
@@ -10,6 +11,7 @@
1011
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1112
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
1213
import org.springframework.security.web.access.AccessDeniedHandler;
14+
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
1315
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
1416

1517
@Configuration
@@ -26,35 +28,36 @@ public SecSecurityConfig() {
2628
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
2729
// @formatter:off
2830
auth.inMemoryAuthentication()
29-
.withUser("user1").password("user1Pass").roles("USER")
30-
.and()
31-
.withUser("user2").password("user2Pass").roles("USER")
32-
.and()
33-
.withUser("admin").password("adminPass").roles("ADMIN");
31+
.withUser("user1").password("user1Pass").roles("USER")
32+
.and()
33+
.withUser("user2").password("user2Pass").roles("USER")
34+
.and()
35+
.withUser("admin").password("adminPass").roles("ADMIN");
3436
// @formatter:on
3537
}
3638

3739
@Override
3840
protected void configure(final HttpSecurity http) throws Exception {
3941
// @formatter:off
4042
http
41-
.csrf().disable()
42-
.authorizeRequests()
43-
.antMatchers("/admin/**").hasRole("ADMIN")
44-
.antMatchers("/anonymous*").anonymous()
45-
.antMatchers("/login*").permitAll()
46-
.anyRequest().authenticated()
47-
.and()
48-
.formLogin()
49-
.loginPage("/login.html")
50-
.loginProcessingUrl("/perform_login")
51-
.defaultSuccessUrl("/homepage.html",true)
52-
.failureUrl("/login.html?error=true")
53-
.and()
54-
.logout()
55-
.logoutUrl("/perform_logout")
56-
.deleteCookies("JSESSIONID")
57-
.logoutSuccessHandler(logoutSuccessHandler());
43+
.csrf().disable()
44+
.authorizeRequests()
45+
.antMatchers("/admin/**").hasRole("ADMIN")
46+
.antMatchers("/anonymous*").anonymous()
47+
.antMatchers("/login*").permitAll()
48+
.anyRequest().authenticated()
49+
.and()
50+
.formLogin()
51+
.loginPage("/login.html")
52+
.loginProcessingUrl("/perform_login")
53+
.defaultSuccessUrl("/homepage.html", true)
54+
//.failureUrl("/login.html?error=true")
55+
.failureHandler(authenticationFailureHandler())
56+
.and()
57+
.logout()
58+
.logoutUrl("/perform_logout")
59+
.deleteCookies("JSESSIONID")
60+
.logoutSuccessHandler(logoutSuccessHandler());
5861
//.and()
5962
//.exceptionHandling().accessDeniedPage("/accessDenied");
6063
//.exceptionHandling().accessDeniedHandler(accessDeniedHandler());
@@ -71,4 +74,8 @@ public AccessDeniedHandler accessDeniedHandler() {
7174
return new CustomAccessDeniedHandler();
7275
}
7376

77+
@Bean
78+
public AuthenticationFailureHandler authenticationFailureHandler() {
79+
return new CustomAuthenticationFailureHandler();
80+
}
7481
}

‎spring-security-mvc-login/src/main/resources/webSecurityConfig.xml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@
1515

1616
<csrf disabled="true"/>
1717

18-
<form-login login-page='/login.html' login-processing-url="/perform_login" default-target-url="/homepage.html" authentication-failure-url="/login.html?error=true"
19-
always-use-default-target="true"/>
18+
<form-login login-page='/login.html' login-processing-url="/perform_login" default-target-url="/homepage.html"
19+
always-use-default-target="true" authentication-failure-handler-ref="authenticationFailureHandler"/>
2020

2121
<logout logout-url="/perform_logout" delete-cookies="JSESSIONID" success-handler-ref="customLogoutSuccessHandler"/>
2222

2323
<!-- <access-denied-handler error-page="/accessDenied"/> -->
2424
<access-denied-handler ref="customAccessDeniedHandler"/>
25-
25+
2626
</http>
2727

2828
<beans:bean name="customLogoutSuccessHandler" class="org.baeldung.security.CustomLogoutSuccessHandler"/>
2929

3030
<beans:bean name="customAccessDeniedHandler" class="org.baeldung.security.CustomAccessDeniedHandler" />
3131

32+
<beans:bean id="authenticationFailureHandler" name="customAuthenticationFaiureHandler" class="org.baeldung.security.CustomAuthenticationFailureHandler"/>
33+
3234
<authentication-manager>
3335
<authentication-provider>
3436
<user-service>

0 commit comments

Comments
 (0)