4

我无法使用 Springs Security 保护两个 URL。我想保护 /admin/** 和 /user/**。主要问题是我有单独的 ADMIN 表作为 ADMIN & ADMIN_ROLES 和 USER 作为 USER & USER_ROLES。我也有单独的管理员和用户登录页面。我在下面分享我的代码。请帮我解决这个问题。

我需要的是,当一些进入 /admin URL 时,它应该显示 Admin Login Page 并将我重定向到 /admin/embassy,当一些打开 / URL 它应该到 User Login Page 并且成功登录时应该将我重定向到 /user/embassy .

弹簧安全.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http auto-config="true">
    <intercept-url pattern="/user/**" access="ROLE_USER" />
    <form-login login-page="/" default-target-url="/user/embassy"
        authentication-failure-url="/loginfailed" />
    <logout invalidate-session="true" logout-success-url="/logout" />
</http>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="select USERNAME, PASSWORD, ENABLED from USER where USERNAME = ?"
            authorities-by-username-query="select u.USERNAME, ur.AUTHORITY from USER u, USER_ROLES ur where u.ID = ur.USER_ID and u.USERNAME =?"
        />
    </authentication-provider>
</authentication-manager>

<http auto-config="true">
    <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
    <form-login login-page="/admin" default-target-url="/admin/embassy"
        authentication-failure-url="/adminloginfailed" />
    <logout invalidate-session="true" logout-success-url="/adminlogout" />
</http>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="select USERNAME, PASSWORD, ENABLED from ADMIN where USERNAME = ?"
            authorities-by-username-query="select a.USERNAME, ar.AUTHORITY from ADMIN a, ADMIN_ROLES ar where a.ID = ar.USER_ID and a.USERNAME =?"
        />
    </authentication-provider>
</authentication-manager>

web.xml

    <?xml version="1.0" encoding="utf-8" standalone="no"?><web-app 
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>Admin</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Admin</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>User</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>User</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
              org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/User-servlet.xml,
                    /WEB-INF/Admin-servlet.xml,
        /WEB-INF/Spring-Datasource.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

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

<welcome-file-list>
    <welcome-file>index</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>SystemServiceServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
    <init-param>
        <param-name>services</param-name>
        <param-value/>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>SystemServiceServlet</servlet-name>
    <url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>

4

2 回答 2

3

阅读评论后,我猜你对一张桌子没有问题,所以我假设它。
所以你spring-security.xml应该看起来像:

  <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http auto-config="true">
    <intercept-url pattern="/user/**" access="ROLE_USER" />
    <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
    <form-login login-page="/" authentication-success-handler-ref="mySuccessHandler"
        authentication-failure-url="/loginfailed" />
    <logout invalidate-session="true" logout-success-url="/logout" />
</http>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="select USERNAME, PASSWORD, ENABLED from USER where USERNAME = ?"
            authorities-by-username-query="select u.USERNAME, ur.AUTHORITY from USER u, USER_ROLES ur where u.ID = ur.USER_ID and u.USERNAME =?" />
    </authentication-provider>
</authentication-manager>

我假设您的所有用户和管理员现在都在用户表中。
现在实现。AuthenticationSuccessHandler,并将其注册为与 aboves 的值同名的 bean athentication-success-handler-ref,重定向到您想要的页面。

public class MySuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
            HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN")){
            response.sendRedirect("/admin/embassy");   
            return;
        }
        response.sendRedirect("/user/embassy");
    }    
}

所以我们添加到spring-security.xml

<beans:bean id="mySuccessHandler" class="my.domain.MySuccessHandler" />
于 2012-12-26T07:55:05.223 回答
3

您正在尝试获得一个非常定制的身份验证方案,您需要绕过许多 http命名空间功能才能使其工作。您将需要一些东西:

如您所见,这需要一些工作。如果可能,我强烈建议对用户使用统一的表结构和统一的登录页面。这将为您提供一个非常标准的 Spring Security 配置。然后,您可以稍微自定义该配置,以便在登录成功时根据用户角色动态重定向。

于 2012-12-26T08:08:26.100 回答