即使使用 create-session="stateless",Spring Security Oauth2 也会生成 jsessiond
Posted
技术标签:
【中文标题】即使使用 create-session="stateless",Spring Security Oauth2 也会生成 jsessiond【英文标题】:Spring Security Oauth2 generating jsessiond even when create-session="stateless" is used 【发布时间】:2014-01-20 18:15:34 【问题描述】:我在我的无状态应用程序中使用 Spring Security Oauth2 进行身份验证。下面是 spring 配置文件的代码 sn-p
此外,我在所有 jsps 中都使用了<%@ page session="false" %>
。
<http pattern="/oauth/token" create-session="stateless"
authentication-manager-ref="clientAuthenticationManager"
xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
<anonymous enabled="false" />
<http-basic entry-point-ref="clientAuthenticationEntryPoint" />
<!-- include this only if you need to authenticate clients via request -->
<!-- parameters -->
<custom-filter ref="clientCredentialsTokenEndpointFilter"
after="BASIC_AUTH_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
<http auto-config="true" create-session="stateless">
<intercept-url pattern="/oauth/**" access="ROLE_USER" />
<intercept-url pattern="/welcome*" access="ROLE_USER" />
<intercept-url pattern="/test" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed"
authentication-success-handler-ref="customAuthenticationSuccessHandler" />
<logout logout-success-url="/logout" />
<custom-filter ref="preAuthFilter" after="PRE_AUTH_FILTER" />
<custom-filter ref="oauth2ClientFilter" after="EXCEPTION_TRANSLATION_FILTER" />
</http>`
我还创建了自己的授权端点 (/authorizeTest),因为 Ouath2(/oauth/authorize) 提供的授权端点将 AuthorizationRequest 作为会话属性。以下是 CustomAuthorizationEndPoint 的代码 sn-p
<beans:bean id="customAuthorizationEndpoint"
class="com.mkyong.common.controller.CustomAuthorizationEndpoint">
<beans:property name="tokenGranter" ref="authorizationCodeTokenGranter" />
<beans:property name="clientDetailsService" ref="clientDetails" />
<beans:property name="oAuth2RequestFactory" ref="customOAuth2RequestFactory" />
<beans:property name="authorizationCodeServices"
ref="inMemoryAuthorizationCodeServices" />
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
<beans:bean id="authorizationCodeTokenGranter"
class="org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter">
<beans:constructor-arg index="0" ref="tokenServices" />
<beans:constructor-arg index="1"
ref="authorizationCodeServices" />
<beans:constructor-arg index="2" ref="clientDetails" />
<beans:constructor-arg index="3"
ref="customOAuth2RequestFactory" />
</beans:bean>
<beans:bean id="customOAuth2RequestFactory"
class="com.mkyong.common.controller.CustomOAuth2RequestFactory">
<beans:constructor-arg ref="clientDetails" />
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="customAuthorizationRequest" ref="customAuthorizationRequest" />
</beans:bean>
<beans:bean id="customAuthorizationRequest"
class="com.mkyong.common.controller.CustomAuthorizationRequest">
</beans:bean>
<beans:bean id="authorizationCodeServices"
class="org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices">
<beans:constructor-arg ref="dataSource" />
</beans:bean>
但我仍然在生成 jsession。
【问题讨论】:
【参考方案1】:使用create-session="stateless"
意味着您告诉Spring Security 不要为用户创建会话或存储身份验证信息。如果他们觉得有必要,它不会阻止其他库创建会话(oauth 是一个单独的项目)。
我真的不明白您为什么将应用程序标记为无状态,因为您使用的是表单登录之类的东西。如果您不允许创建会话,授权代码流之类的东西将如何工作?授权请求是如何缓存的,当用户被重定向回该请求时,服务器如何知道用户已通过身份验证?身份验证将丢失,因为没有会话可以绑定。
【讨论】:
感谢您的评论。就像 spring security 提供对无状态功能的支持一样,Oauth 2 也提供任何支持,因为他们现在有这么多无状态的应用程序。 OAuth2 规范涵盖了许多不同的用例,而不仅仅是授权代码流。例如,如果一个客户端应用程序正在调用另一个客户端应用程序(典型的无状态情况),则存在“客户端凭据”授权。从您的问题中并不清楚您所说的“您的应用程序”是什么意思。您似乎主要指的是与授权服务器的交互,通常不会是无状态的。 OAuth2 不是身份验证规范,因此当您说您“出于身份验证目的”使用它时,您应该澄清您的意思。您正在使用哪种补助金?用例是什么。 我想使用 Oauth2 来验证用户登录到我的应用程序。这里基本上我是客户端应用程序,同时也是服务器(授权服务器和资源服务器)应用程序。这背后的用例是目前我正在通过我的代码生成令牌并将其保存到数据库但我想要的是Oauth2 生成令牌并在每个用户登录时对其进行验证。另外我目前正在使用授权码授予类型。 根据您的建议,我能够使用带有 client_credentials 授权类型的 OAuth2 以无状态方式登录应用程序。据我了解,OAuth2 提供了一种访问受保护资源的方法。在我登录后的情况下,任何 Ajax 调用都将导致受保护的资源,因此我需要从客户端(javascript)端进行 OAuth 休息调用。为了记录用户,我可以使用以上是关于即使使用 create-session="stateless",Spring Security Oauth2 也会生成 jsessiond的主要内容,如果未能解决你的问题,请参考以下文章
为啥即使使用 hibernate.hbm2ddl.auto" value="create",hibernate 也不会自动创建表?
即使使用 ALLOWED_HOSTS=["*"] 在 django 上使用 debug=False 命中 500 错误
即使在使用 @CrossOrigin(origins="*") 注释控制器后,跨域请求也会被阻止?
为啥我必须使用 set_charset("utf8") 即使一切都是 utf-8 编码的? (MySQLi-PHP)
Firefox:未显示本地托管的网络字体 - 即使使用 Access-Control-Allow-Origin "*"
即使在构建路径中使用 ojdbc14.jar,Class.forName("oracle.jdbc.driver.OracleDriver") 也会得到 ClassNotFound