2014-01-18 52 views
11

我正在使用Spring安全性以用用户名和密码登录到应用程序管理部分。但是现在我的客户端需要为应用程序客户端部分设置另一个登录屏幕,他们将在其中拥有自己的用户名/密码以登录到客户端部分。到目前为止,我已经用下面的弹簧security.xml文件设置实施管理部分成功登录:具有多个登录页面的弹簧安全性

<security:http auto-config="true" use-expressions="true"> 
    <security:form-login login-page="/login" 
     default-target-url="/admin/dashboard" always-use-default-target="true" 
     authentication-failure-url="/login/admin?error_msg=wrong username or password" /> 
    <security:intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')" />   
    <security:logout logout-success-url="/login"/> 
</security:http> 

<security:authentication-manager> 
    <security:authentication-provider 
     user-service-ref="adminServiceImpl"> 
    </security:authentication-provider> 
</security:authentication-manager> 

我在网上搜索了很多试图找到如何添加客户端部分的登录界面, intercept-url(s),安全认证提供程序,但找不到任何信息,那么有人可以帮助我找到任何教程/示例的链接,指导如何做到这一点?

由于

回答

5

按照Spring Security docs

从春季安全3.1,现在有可能使用多个HTTP 元素来定义用于 不同请求模式单独的安全过滤器链配置。如果pattern属性从 中省略了一个http元素,它将匹配所有请求。

每个元素在内部FilterChainProxy中创建一个过滤器链,并且应该映射到它的URL模式。元素将按照它们声明的顺序添加,因此必须首先声明最具体的模式。

所以,基本上你需要两个<http>元素,每个元素具有不同的pattern属性。

有一个详细的教程在这里:https://blog.codecentric.de/en/2012/07/spring-security-two-security-realms-in-one-application/

2

我会只用一个security:http,但注册两个UsernamePasswordLoginFilter秒。

如果两个登录页面记录到相同的安全领域,此解决方案将是适当的。 (因此,如果用户登录的登录页面无关紧要)。当然,对于不同类型的用户,您仍然可以使用角色来限制应用程序不同部分的访问权限。

该解决方案应该很容易,因为您不需要处理两个security:http部分。

这样做的一个主要缺点是:如果他尝试访问需要登录的页面,则必须决定两个登录页面中的哪一个登录的用户被重定向。

+0

非常感谢您的回复。不幸的是我所寻求的是有两个登录页面,每个人都有自己独立的用户表中DB,所以我想我必须遵循两个安全:HTTP方法。 – MChan

1

具有多个登录表单的Spring MVC App示例项目。

三种类型的页面普通/会员/管理员。 如果您尝试访问会员页面,则会将其带入会员登录表单。 如果您尝试访问管理页面,请转到管理员登录表单。

https://github.com/eric-mckinley/springmultihttploginforms

利用在seucrity XML配置文件中的正则表达式的蚂蚁请求匹配完成。

<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.xsd"> 

<global-method-security secured-annotations="enabled" /> 

<http name="member" pattern="/member/*" request-matcher="ant" auto-config="true" use-expressions="false"> 
    <csrf disabled="true"/> 

    <intercept-url pattern="/member/home" access="ROLE_MEMBER" /> 
    <intercept-url pattern="/member/account" access="ROLE_MEMBER" /> 
    <intercept-url pattern="/member/orders" access="ROLE_MEMBER" /> 

    <form-login login-page="/member-login" always-use-default-target="false"/> 
    <logout logout-url="/logout" logout-success-url="/home"/> 
</http> 

<http name="admin" request-matcher="regex" auto-config="true" use-expressions="false"> 
    <csrf disabled="true"/> 

    <intercept-url pattern="/admin/home" access="ROLE_ADMIN" /> 
    <intercept-url pattern="/admin/users" access="ROLE_ADMIN" /> 

    <form-login login-page="/admin-login" always-use-default-target="false"/> 
    <logout logout-url="/logout" logout-success-url="/home"/> 
</http> 

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="admin" password="password" authorities="ROLE_ADMIN" /> 
      <user name="member" password="password" authorities="ROLE_MEMBER" /> 
      <user name="super" password="password" authorities="ROLE_ADMIN,ROLE_MEMBER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager>