我无法使用 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>