Skip to content

Commit e85fd01

Browse files
author
eugenp
committed
security work
1 parent 794e3c0 commit e85fd01

File tree

6 files changed

+78
-27
lines changed

6 files changed

+78
-27
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.spring.security;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
import org.springframework.security.core.Authentication;
10+
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
11+
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
12+
13+
public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {
14+
15+
public CustomLogoutSuccessHandler() {
16+
super();
17+
}
18+
19+
// API
20+
21+
@Override
22+
public void onLogoutSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
23+
final String refererUrl = request.getHeader("Referer");
24+
System.out.println(refererUrl);
25+
26+
super.onLogoutSuccess(request, response, authentication);
27+
}
28+
29+
}

spring-security-login/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,30 @@
1313
@Configuration
1414
public class ClientWebConfig extends WebMvcConfigurerAdapter {
1515

16-
public ClientWebConfig() {
17-
super();
18-
}
16+
public ClientWebConfig() {
17+
super();
18+
}
1919

20-
// API
20+
// API
2121

22-
@Override
23-
public void addViewControllers(final ViewControllerRegistry registry) {
24-
super.addViewControllers(registry);
22+
@Override
23+
public void addViewControllers(final ViewControllerRegistry registry) {
24+
super.addViewControllers(registry);
2525

26-
registry.addViewController("/login.html");
27-
registry.addViewController("/homepage.html");
28-
}
26+
registry.addViewController("/anonymous.html");
2927

30-
@Bean
31-
public ViewResolver viewResolver() {
32-
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
28+
registry.addViewController("/login.html");
29+
registry.addViewController("/homepage.html");
30+
}
3331

34-
bean.setViewClass(JstlView.class);
35-
bean.setPrefix("/WEB-INF/view/");
36-
bean.setSuffix(".jsp");
32+
@Bean
33+
public ViewResolver viewResolver() {
34+
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
3735

38-
return bean;
39-
}
36+
bean.setViewClass(JstlView.class);
37+
bean.setPrefix("/WEB-INF/view/");
38+
bean.setSuffix(".jsp");
39+
40+
return bean;
41+
}
4042
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
77

8-
<http use-expressions="true">
9-
<intercept-url pattern="/login*" access="permitAll" />
10-
<intercept-url pattern="/**" access="isAuthenticated()" />
8+
<debug/>
9+
10+
<http use-expressions="true" >
11+
<intercept-url pattern="/anonymous*" access="isAnonymous()" />
12+
13+
<intercept-url pattern="/login*" access="permitAll" />
14+
15+
<intercept-url pattern="/**" access="isAuthenticated()" />
1116

1217
<form-login
1318
login-page='/login.html'
@@ -16,9 +21,14 @@
1621
authentication-failure-url="/login.html?error=true"
1722
always-use-default-target="true"/>
1823

19-
<logout/>
20-
24+
<logout
25+
logout-url="/perform_logout"
26+
delete-cookies="JSESSIONID"
27+
success-handler-ref="customLogoutSuccessHandler" />
28+
2129
</http>
30+
31+
<beans:bean name="customLogoutSuccessHandler" class="org.baeldung.spring.security.CustomLogoutSuccessHandler" />
2232

2333
<authentication-manager>
2434
<authentication-provider>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<html>
3+
<head></head>
4+
5+
<body>
6+
<h1>Anonymous page</h1>
7+
8+
<a href="<c:url value="/login.html" />">To Login</a>
9+
</body>
10+
</html>

spring-security-login/src/main/webapp/WEB-INF/view/homepage.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
<body>
66
<h1>This is the body of the sample view</h1>
7-
<a href="<c:url value="j_spring_security_logout" />"> Logout</a>
7+
<a href="<c:url value="/perform_logout" />">Logout</a>
88
</body>
99
</html>

spring-security-login/src/main/webapp/WEB-INF/web.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
<url-pattern>/*</url-pattern>
4646
</filter-mapping>
4747

48-
<welcome-file-list>
49-
<welcome-file>index.html</welcome-file>
50-
</welcome-file-list>
48+
<!-- <welcome-file-list> -->
49+
<!-- <welcome-file>index.html</welcome-file> -->
50+
<!-- </welcome-file-list> -->
5151

5252
</web-app>

0 commit comments

Comments
 (0)