我是春天新手,在我的项目中,我需要通过Spring安全性向管理员和用户添加两个登录表单。到目前为止,我能够成功创建一个登录页面。这是我的 的 弹簧security.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-3.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/welcome*" access="isAnonymous()"/>
<intercept-url pattern="/signup*" access="isAnonymous()"/>
<!--<intercept-url pattern="/login*" access="isAnonymous()" />-->
<intercept-url pattern="/selection" access="isAuthenticated()"/>
<intercept-url pattern="/dashboard" access="isAuthenticated()"/>
<!-- access denied page -->
<access-denied-handler error-page="/403" />
<form-login
login-page="/login"
default-target-url="/selection"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>
<!-- Select users and user_roles from database -->
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService" >
<password-encoder hash="plaintext" />
</authentication-provider>
</authentication-manager>
<beans:bean id="myUserDetailsService" class="com.cse.cloud4s.service.MyUserDetailsService"/>
</beans:beans>
的web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml,
/WEB-INF/spring-database.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>
如何修改代码以使用多个登录页面?
答案 0 :(得分:5)
您可以拥有任意数量的登录页面,但只有一个默认登录页面,如果用户未经过身份验证,则弹出安全性会重定向到该页面 - 无论如何,在验证之前很难猜测 如果用户想以管理员身份登录。
唯一的规则是所有登录页面必须向同一个网址提交相同的字段,并且该网址由spring security处理。
我唯一的问题是为什么你需要多个登录页面? Spring安全方式是将权限附加到登录名,而不是您登录的方式。
答案 1 :(得分:2)
从Spring Security 3.1开始,现在可以使用多个http 用于定义单独的安全过滤器链配置的元素 不同的请求模式。如果省略pattern属性 一个http元素,它匹配所有请求。创建一个不安全的 pattern是这种语法的一个简单示例,其中模式是 映射到空的过滤器链。
有关详细信息,请参阅this Spring Security Documenttation