我无法使用Springs Security保护两个网址。我想要保护/ admin / **和/ user / **。主要问题是我有ADMIN的单独表作为ADMIN& ADMIN_ROLES和USER as USER& USER_ROLES。我也有管理员和用户的单独登录页面。我在下面分享我的代码。请帮我解决这个问题。
我需要的是当有人进入/ admin网址时,它应该显示管理员登录页面并将我重定向到/ admin / embassy,当一些打开/ URL应该到用户登录页面并且成功登录时应该将我重定向到/用户/大使馆。
弹簧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" />
<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>
答案 0 :(得分: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>
我假设您的所有用户和管理员现在都在用户表中
现在impl。 AuthenticationSuccessHandler
,并将其注册为与上面athentication-success-handler-ref
的名称相同的bean,重定向到您想要的页面。
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" />
答案 1 :(得分:3)
您正在尝试获得一个非常自定义的身份验证方案,您需要绕过许多http
命名空间功能才能使其正常工作。你需要的一些东西:
AuthenticationEntryPoint
个实例,每个登录页面一个AuthenticationDetailsSource
注入自定义UsernamePasswordAuthenticationFilter
,以便将一些上下文存储到Authentication
个实例中,以便您确定用户登录的登录页面是否为ADMIN one或USER one。AuthenticationManager
检索该上下文的自定义Authentication.getDetails()
实例,并决定对ADMIN访问权限或USER访问权限进行身份验证。DaoAuthenticationProviders
,每个JdbcDaoImpl
,一个定义ADMIN表的查询,另一个定义USER表的查询。如您所见,这需要一些工作。我强烈建议为用户使用统一的表结构,并尽可能使用统一的登录页面。这将为您提供非常标准的Spring Security配置。然后,您可以稍微自定义该配置,以便在登录成功的情况下根据用户角色动态重定向。