如果試圖訪問一個沒有訪問權限的頁面,那么頁面會出現403 訪問錯誤。
如果我們希望自定義訪問拒絕頁面,只需要隨便創建一個jsp頁面,讓后將這個頁面的位置放到配置文件中。
下面創建一個accessDenied.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Access Denied</title>
<style type="text/css">
div.error {
width: 260px;
border: 2px solid red;
background-color: yellow;
text-align: center;
}
</style>
</head>
<body>
<h1>Access Denied</h1>
<hr>
<div class="error">
訪問被拒絕<br>
${requestScope['SPRING_SECURITY_403_EXCEPTION'].message}
</div>
<hr>
</body>
</html>
修改配置文件
<http auto-config='true' access-denied-page="/accessDenied.jsp">
<intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" />
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
