Skip to content

Commit 424f55a

Browse files
lor6KevinGilmore
authored andcommitted
custom access denied page (eugenp#1133)
* custom access denied page * fix formatting, remove imports
1 parent c6c3b71 commit 424f55a

7 files changed

Lines changed: 82 additions & 11 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.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.apache.log4j.Logger;
10+
import org.springframework.security.access.AccessDeniedException;
11+
import org.springframework.security.core.Authentication;
12+
import org.springframework.security.core.context.SecurityContextHolder;
13+
import org.springframework.security.web.access.AccessDeniedHandler;
14+
15+
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
16+
17+
public static final Logger LOG = Logger.getLogger(CustomAccessDeniedHandler.class);
18+
19+
@Override
20+
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exc) throws IOException, ServletException {
21+
Authentication auth = SecurityContextHolder.getContext()
22+
.getAuthentication();
23+
if (auth != null) {
24+
LOG.warn("User: " + auth.getName() + " attempted to access the protected URL: " + request.getRequestURI());
25+
}
26+
27+
response.sendRedirect(request.getContextPath() + "/accessDenied");
28+
}
29+
30+
}

‎spring-security-mvc-login/src/main/java/org/baeldung/spring/MvcConfig.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void addViewControllers(final ViewControllerRegistry registry) {
2828
registry.addViewController("/login.html");
2929
registry.addViewController("/homepage.html");
3030
registry.addViewController("/admin/adminpage.html");
31+
registry.addViewController("/accessDenied");
3132
}
3233

3334
@Bean

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

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

3+
import org.baeldung.security.CustomAccessDeniedHandler;
34
import org.baeldung.security.CustomLogoutSuccessHandler;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
@@ -8,6 +9,7 @@
89
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
910
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1011
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12+
import org.springframework.security.web.access.AccessDeniedHandler;
1113
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
1214

1315
@Configuration
@@ -53,12 +55,20 @@ protected void configure(final HttpSecurity http) throws Exception {
5355
.logoutUrl("/perform_logout")
5456
.deleteCookies("JSESSIONID")
5557
.logoutSuccessHandler(logoutSuccessHandler());
58+
//.and()
59+
//.exceptionHandling().accessDeniedPage("/accessDenied");
60+
//.exceptionHandling().accessDeniedHandler(accessDeniedHandler());
5661
// @formatter:on
5762
}
5863

5964
@Bean
6065
public LogoutSuccessHandler logoutSuccessHandler() {
6166
return new CustomLogoutSuccessHandler();
6267
}
68+
69+
@Bean
70+
public AccessDeniedHandler accessDeniedHandler(){
71+
return new CustomAccessDeniedHandler();
72+
}
6373

6474
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
always-use-default-target="true"/>
2020

2121
<logout logout-url="/perform_logout" delete-cookies="JSESSIONID" success-handler-ref="customLogoutSuccessHandler"/>
22+
23+
<!-- <access-denied-handler error-page="/accessDenied"/> -->
24+
<access-denied-handler ref="customAccessDeniedHandler"/>
25+
2226
</http>
2327

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

30+
<beans:bean name="customAccessDeniedHandler" class="org.baeldung.security.CustomAccessDeniedHandler" />
31+
2632
<authentication-manager>
2733
<authentication-provider>
2834
<user-service>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Access Denied</title>
9+
</head>
10+
<body>
11+
<h2>Sorry, you do not have permission to view this page.</h2>
12+
13+
Click <a href="<c:url value="/homepage.html" /> ">here</a> to go back to the Homepage.
14+
</body>
15+
</html>

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
<head></head>
55

66
<body>
7-
<h1>This is the body of the sample view</h1>
7+
<h1>This is the body of the sample view</h1>
88

9-
<security:authorize access="hasRole('ROLE_USER')">
10-
This text is only visible to a user
11-
<br/>
12-
</security:authorize>
9+
<security:authorize access="hasRole('ROLE_USER')">
10+
This text is only visible to a user
11+
<br/> <br/>
12+
<a href="<c:url value="/admin/adminpage.html" />">Restricted Admin Page</a>
13+
<br/> <br/>
14+
</security:authorize>
1315

14-
<security:authorize access="hasRole('ROLE_ADMIN')">
15-
This text is only visible to an admin
16-
<br/>
16+
<security:authorize access="hasRole('ROLE_ADMIN')">
17+
This text is only visible to an admin
18+
<br/>
1719
<a href="<c:url value="/admin/adminpage.html" />">Admin Page</a>
1820
<br/>
19-
</security:authorize>
21+
</security:authorize>
22+
23+
<a href="<c:url value="/perform_logout" />">Logout</a>
2024

21-
<a href="<c:url value="/perform_logout" />">Logout</a>
22-
2325
</body>
2426
</html>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@
4343
<url-pattern>/*</url-pattern>
4444
</filter-mapping>
4545

46+
<error-page>
47+
<error-code>403</error-code>
48+
<location>/accessDenied</location>
49+
</error-page>
50+
4651
<!-- <welcome-file-list> -->
4752
<!-- <welcome-file>index.html</welcome-file> -->
4853
<!-- </welcome-file-list> -->
54+
55+
4956

5057
</web-app>

0 commit comments

Comments
 (0)