私はスプリングウェブのセキュリティを理解しようとしています。そのために、ログインページと2人の異なるユーザーを持つWebアプリケーションを作成しました。スプリングウェブセキュリティ:ログイン後にページにアクセスできない
- 管理
- ユーザー/ユーザーレビュー
LoginControllerクラスは、URLの3種類を扱います。春のセキュリティで
/*
Used to guide the user to login page
*/
@RequestMapping(value="/login/login.htm", method=RequestMethod.GET)
public ModelAndView login(){
ModelAndView modelAndView = new ModelAndView("login");
System.out.println("Rendering login page.................");
return modelAndView;
}
/*
Process the successful login and redirects user to Admin/User page as per the role.
*/
@RequestMapping(value="/login/success")
public ModelAndView loginSuccess(HttpServletRequest request){
ModelAndView modelAndView = new ModelAndView();
Set<String> roles = AuthorityUtils
.authorityListToSet(SecurityContextHolder.getContext()
.getAuthentication().getAuthorities());
System.out.println("ROLES: "+roles);
if(roles.contains("USER_ADMIN")){
modelAndView.setViewName("admin");
}else{
modelAndView.setViewName("user");
}
return modelAndView;
}
/*
Admin page has a hyper link to access the manage user page. It is handled here.
*/
@RequestMapping(value="/admin/manageUser.htm", method=RequestMethod.GET)
public ModelAndView manageUser(){
ModelAndView modelAndView = new ModelAndView("manageUser");
return modelAndView;
}
web.xmlの
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/SecurityConfig.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring Security --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
私は '管理/' & 'ユーザー/' ですべてのURLを制限しています。
<http auto-config="true"> <intercept-url pattern="/admin/**" access="hasRole('USER_ADMIN')"/> <intercept-url pattern="/user/**" access="hasRole('USER_GUEST')"/> <form-login login-page="/login/login.htm" username-parameter="userName" password-parameter="password" login-processing-url="/j_spring_security_check" authentication-success-forward-url="/login/success"/> <remember-me/> </http> <authentication-manager> <authentication-provider> <user-service> <user name="renju" password="12345" authorities="USER_ADMIN"/> <user name="guest" password="guest" authorities="USER_GUEST"/> </user-service> </authentication-provider> </authentication-manager>
i 'はhttp://localhost:8080/SpringWebSecurityThree-0.0.1-SNAPSHOT/login/login.htm' しようとすると、アプリケーションは、カスタムログインページを開きます。
管理者のユーザー名とパスワードを入力すると、アプリケーションは管理ページを開きます。
i 'はManageUser' をクリックしたときに今、私はページをmanageuser tothe私を取るために、アプリケーションを期待しています。しかし、それは 'アクセス拒否'と言います。
は、私はそれがインターセプト-URLの与えられたとは何かを持っていると信じています。
この問題を解決する手助けをしてください。私はあまりにもJSPページを掲載しています
..
login.jspを
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${not empty error}">
<c:out value="${error}"></c:out>
</c:if>
<form action="../j_spring_security_check" method="post">
<table>
<tr>
<td>UserName</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="LOGIN"></td>
</tr>
</table>
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
</body>
</html>
admin.jspに
manageuser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page session="true" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Managing User....
<br/>
Logged In UserType:
<%
out.println(session.getAttribute("userType"));
%>
</body>
</html>
ありがとうございます。私はそれを意図的に保たなかった。私はこの問題を解決するためにhttpタグで遊んでいる間、私はそれを保った。また、 'create-session =' always 'を試みました。それも働かなかった。それを削除しました。 – Renjith
@Renjuエラーログを入力して問題を特定してください。 – ScanQR
JBossコンソールにエラーログがありません。私は、それがブロックされていることを私がセキュリティ設定に保存した状態であると信じています。だからエラーを出すことはない。 – Renjith