Spring Security OAuth2 /oauth/token endpoint after Basic auth success then no access token

ぐ巨炮叔叔 提交于 2019-12-14 03:17:47

问题


I am adding spring security oauth2 to my spring boot application. I have followed a few of the examples on the web and in github from the spring team (had to make some mods for my use case), but I am still not able to return an oauth2 access token from the /oauth/token endpoint. I have been working on this for several days and started off trying to do it in JavaConfig, but then switched to xml config and was able to make some progress. I should note the only config I am doing in xml is the security config and related security beans, all other config is done via JavaConfig.

I am able to successfully authenticate my user (using username & password) using Basic auth, but when it comes to the next step to generate and return the bearer token, this is not happening. Note, I am using a database for storing my user credentials and to store my access tokens (once they get created).

I am making a POST request using grant_type=password with a client id and client secret.

I am under the impression spring oauth2 would handle creating the access token for me and returning it, but maybe this isn't correct or my xml config maybe wrong? Many thanks for any assistance!

Here is my security config xml below:

<http pattern="/oauth/token" 
    create-session="stateless"        
    authentication-manager-ref="authenticationManager"
    xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/**" method="GET" access="ROLE_DENY" />
    <intercept-url pattern="/**" method="PUT" access="ROLE_DENY" />
    <intercept-url pattern="/**" method="DELETE" access="ROLE_DENY" />
    <intercept-url pattern="/**" 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 pattern="/**" create-session="never" 
    entry-point-ref="oauthAuthenticationEntryPoint" 
    xmlns="http://www.springframework.org/schema/security">
    <!-- authentication-manager-ref="authenticationManager"
    access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security"-->
    <anonymous enabled="false" />
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" />
    <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>

<bean id="oauthAuthenticationEntryPoint"
    class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    <!-- property name="realmName" value="f2rRealm" /-->
</bean>

<bean id="clientAuthenticationEntryPoint"
    class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    <property name="realmName" value="f2r/client" />
    <property name="typeName" value="Basic" />
</bean>

<bean id="clientCredentialsTokenEndpointFilter"
    class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
    <property name="authenticationManager" ref="authenticationManager" />
</bean>

<bean id="oauthAccessDeniedHandler"
    class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" />

<authentication-manager alias="authenticationManager"
    xmlns="http://www.springframework.org/schema/security" >
    <!-- authentication-provider user-service-ref="clientDetailsUserService"  /-->
    <authentication-provider ref="authenticationProvider" />
</authentication-manager>

<bean id="clientDetails" class="com.f2r.security.oauth2.F2RJdbcClientDetailsService" >
    <constructor-arg ref="dataSource" />
    <property name="clientId" value="f2r" />
    <property name="clientSecret" value="f2rsecret" />
    <property name="passwordEncoder" ref="passwordEncoder" />
</bean>

<!-- bean id="clientDetailsService"
    class="com.f2r.security.oauth2.F2RJdbcClientDetailsService">
    <constructor-arg ref="dataSource" />
    <property name="clientDetails" ref="clientDetails" />
    <property name="passwordEncoder" ref="passwordEncoder" />
</bean-->

<bean id="clientDetailsUserService"
    class="com.f2r.security.oauth2.F2RClientDetailsUserDetailsService">
    <constructor-arg ref="clientDetails" />
    <property name="passwordEncoder" ref="passwordEncoder" />
</bean>    


<authentication-manager id="userAuthenticationManager"
    xmlns="http://www.springframework.org/schema/security">
    <!-- authentication-provider ref="customUserAuthenticationProvider" /-->
    <authentication-provider ref="authenticationProvider" />
</authentication-manager>

<!-- bean id="customUserAuthenticationProvider" class="com.f2r.security.F2RAuthenticationProvider">
    <property name="userDetailsService" ref="userDetailsService" />
</bean-->

<bean id="authenticationProvider" class="com.f2r.security.F2RAuthenticationProvider">
    <!-- property name="userDetailsService" ref="userDetailsService" /-->
    <property name="userDetailsService" ref="clientDetailsUserService" />
</bean>

<bean id="userDetailsService" class="com.f2r.security.F2RUserDetailsService" />

<bean id="userApprovalHandler" 
    class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler">
    <property name="tokenStore" ref="tokenStore" />
    <property name="clientDetailsService" ref="clientDetails" /> 
    <property name="requestFactory" ref="requestFactory" />  
</bean>

<bean id="requestFactory"
    class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory">
    <constructor-arg ref="clientDetails" />
</bean>

<oauth:authorization-server
    client-details-service-ref="clientDetails" 
    token-services-ref="tokenServices" 
    token-endpoint-url="/oauth/token" 
    authorization-endpoint-url="/oauth/authorize" >
    <oauth:implicit />
    <oauth:refresh-token />
    <oauth:client-credentials />
    <!-- oauth:password authentication-manager-ref="userAuthenticationManager" /-->
    <oauth:password />
</oauth:authorization-server>

<oauth:resource-server id="resourceServerFilter"
    token-services-ref="tokenServices" />

<bean id="tokenStore"
    class="org.springframework.security.oauth2.provider.token.store.JdbcTokenStore" >
    <constructor-arg ref="dataSource"/>
</bean>

<bean id="tokenServices"
    class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
    <property name="tokenStore" ref="tokenStore" />
    <property name="supportRefreshToken" value="true" />
    <property name="clientDetailsService" ref="clientDetails" />
</bean>     

<mvc:annotation-driven />

<mvc:default-servlet-handler />

<sec:global-method-security
    pre-post-annotations="enabled" proxy-target-class="true">
    <!--you could also wire in the expression handler up at the layer of the 
        http filters. See https://jira.springsource.org/browse/SEC-1452 -->
    <sec:expression-handler ref="oauthExpressionHandler" />
</sec:global-method-security>

<oauth:expression-handler id="oauthExpressionHandler" />

<oauth:web-expression-handler id="oauthWebExpressionHandler" />

来源:https://stackoverflow.com/questions/32838172/spring-security-oauth2-oauth-token-endpoint-after-basic-auth-success-then-no-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!