【发布时间】:2014-11-17 09:15:13
【问题描述】:
最近我正在为我的 REST 服务使用 Spring Security 基本身份验证。
下面是安全xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<security:http pattern="/rest/**" create-session="never" use-expressions="true">
<security:http-basic />
<security:intercept-url pattern="/rest/auth/**" access="isAuthenticated()"/>
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="admin" authorities="ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
<security:authentication-provider user-service-ref="userDetailsService">
<security:password-encoder hash="sha-256" />
</security:authentication-provider>
</security:authentication-manager>
<security:global-method-security jsr250-annotations="enabled" pre-post-annotations="enabled"/>
</beans>
我使用的 Spring 框架和 Spring Security:
<springframework.version>4.1.0.RELEASE</springframework.version>
<spring.security.version>3.2.5.RELEASE</spring.security.version>
我将我的 REST 服务映射到前缀为“rest/”的 URL,当我第一次访问该 URL 时,浏览器会提示基本身份验证的用户名和密码字段。我用正确的凭据填充它,我的控制器成功访问。
但是如果我再次尝试使用浏览器访问相同的URL,它不会再次提示我基本认证的用户名和密码字段,而是直接访问该URL。
我希望浏览器总是提示我进行基本身份验证,因为我将 create-session 属性设置为 never。
那么,我错过了什么吗?
【问题讨论】:
标签: java spring-mvc spring-security basic-authentication