当前位置: 首页 > 知识库问答 >
问题:

Spring web security:成功登录后无法访问页面

袁耀
2023-03-14

我试图了解spring-web安全性。为此,我创建了一个具有登录页面和两个不同用户的Web应用程序。

  1. 管理员

LoginController类处理三种类型的URL。

/*
  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;
}

网状物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>

在Spring安全中,我使用“admin/”限制所有网址

<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>

在提供管理员用户名和密码后,应用程序将打开管理员页面。

现在,当我单击“ManageUser”时,我希望应用程序将我带到ManageUser页面。但上面写着“拒绝访问”。

我相信这与给定的截取url有关。

你能帮我解决这个问题吗?

我也在发布jsp页面...

登录名。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>

管理jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page session="true" %>
<!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>
    Admin Page !!!!!!!!!!
    <a href="${pageContext.request.contextPath}/admin/manageUser.htm">ManageUser</a>
    <% session.setAttribute("userType", "admin"); %>
</body>
</html>

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>

共有1个答案

彭鸿文
2023-03-14

您正在使用create session=“never”,这将需要对您的每个请求执行“重新身份验证”。

<http auto-config="true" create-session="never">
  <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>

这通常用于不支持或实施基于cookie/会话的身份验证的情况。确保确定您的策略。

 类似资料:
  • 我使用硒登录网站 https://launch.stellar.org/#/login。但我无法在新页面中获取任何元素。 这里是代码: 线程“main”org.openqa.selenium.InvalidSelectorException中的异常:给定的选择器btn btn-default stellar-按钮ng-绑定无效或不会导致WebElement。发生以下错误:InvalidSelect

  • 面对奇怪的问题:应用程序在HTTP上运行良好,但一旦我们添加SLL证书,就会再次重定向到登录页面:下面是spring-security.xml的代码片段 我已经在JBOSS中独立添加了SSL证书。xml格式如下: 当我尝试使用 HTTPS URL(如 https://mydomain.com:8443/mywebapp/login.rs ) 访问我的应用程序时,它会显示用户名密码页。 输入有效凭据

  • 我在模态窗口中有登录表单。成功登录后,用户被重定向到< code>/页面。我正试图找到一种方法,在登录后留在联系页面或另一个页面。如何做到这一点?我的代码是:

  • 所以我正在尝试在 Spring 启动和安全的帮助下创建简单的登录页面。所以我目前拥有的是自定义登录页面和一个用户和角色的内存身份验证。问题是,当我输入正确的用户/密码时,spirng没有将其认证为有效数据,并再次将我重定向到登录页面,但这次是: 在客户端,我使用胸腔叶。 表单片段: 配置类: 控制器简单 知道我做错了什么吗?

  • 问题内容: 我想要在spring成功注册后自动登录:我有一个受保护的页面,需要登录才能访问它们,我要在注册后跳过登录页面并进行自动登录,以便用户可以看到受保护的页面有我吗?我正在使用spring 3.0,spring security 3.0.2,该怎么做? 问题答案: 可以通过以下方式(半伪代码)通过spring安全性完成此操作: 更新:仅包含注册后如何创建会话

  • 问题内容: 我知道之前曾有人问过这个问题,但是我在这里面临一个特殊的问题。 我使用Spring Security 3.1.3。 我的Web应用程序中有3种可能的登录案例: 通过登录页面登录:确定。 通过受限页面登录:也可以。 通过非受限页面登录:不好,…每个人都可以访问“产品”页面,并且用户可以在登录后发表评论。因此,同一页面中包含一个登录表单,以允许用户进行连接。 情况3)的问题是我无法设法将用