使用@Secured注释时,获取" AuthenticationCredentialsNotFoundException:在SecurityContext"中找不到Authentication对象。

时间:2015-12-09 17:43:56

标签: java spring spring-mvc spring-security spring-security-oauth2

没有@secured注释

工作正常

我的控制器类

@Controller
public class JSONController {

    @Secured({ "ROLE_USER" })
    @RequestMapping(value = "/yagger/{name}", method = RequestMethod.GET)
    public @ResponseBody User getPersonInJSON(@PathVariable String name) {

        User person = new User();
        person.setFirstName(name);

        return person;
    }
}`

弹簧安全

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oauth="http://www.springframework.org/schema/security/oauth2" 
    xmlns:sec="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd
    http://www.springframework.org/schema/security/oauth2 
    http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- Just for testing... -->
    <http pattern="/oauth/cache_approvals" security="none" xmlns="http://www.springframework.org/schema/security" />
    <http pattern="/oauth/uncache_approvals" security="none" xmlns="http://www.springframework.org/schema/security" />


    <!-- The OAuth2 protected resources are separated out into their own block so we can deal with authorization and error handling 
        separately. This isn't mandatory, but it makes it easier to control the behaviour. -->
    <!-- http pattern="/yagger/*" auto-config="true" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint"
        access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false" />
        <intercept-url pattern="/yagger/*" access="ROLE_USER" />
        <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
        <access-denied-handler ref="oauthAccessDeniedHandler" />
    </http-->

    <!-- http pattern="/test/*" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint"
        access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false" />
        <intercept-url pattern="/test/*" access="ROLE_USER" />
        <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
        <access-denied-handler ref="oauthAccessDeniedHandler" />
    </http-->

    <http xmlns="http://www.springframework.org/schema/security" entry-point-ref="oauthAuthenticationEntryPoint"
            authentication-manager-ref="clientAuthenticationManager" create-session="stateless" pattern="/oauth/token">
            <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
            <!-- for PRODUCTION requires-channel="https" -->
            <anonymous enabled="false" />
            <http-basic entry-point-ref="oauthAuthenticationEntryPoint" />
            <!-- include this only if you need to authenticate clients via request 
                parameters -->
            <custom-filter before="BASIC_AUTH_FILTER" ref="clientCredentialsTokenEndpointFilter" />
            <access-denied-handler ref="oauthAccessDeniedHandler" />
        </http>
    <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="yagger" />
    </bean>

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

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

    <bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
        <property name="authenticationManager" ref="clientAuthenticationManager" />
        <property name="authenticationEntryPoint" ref="oauthAuthenticationEntryPoint" />
        <property name="filterProcessesUrl" value="/oauth/token" />
    </bean>
    <bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans">
        <constructor-arg>
            <list>
                <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" />
                <bean class="org.springframework.security.access.vote.RoleVoter" />
                <bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
            </list>
        </constructor-arg>
    </bean>

    <authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider user-service-ref="clientDetailsUserService" />
    </authentication-manager>

    <authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider>
            <user-service id="userDetailsService">
                <user name="user" password="password" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

    <bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
        <constructor-arg ref="clientDetails" />
    </bean>

    <!-- Used for the persistenceof tokens (currently an in memory implementation) -->
    <bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.InMemoryTokenStore" />

    <!-- Used to create token and and every thing about them except for their persistence that is reposibility of TokenStore (Given here is a           default implementation) -->
    <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>

    <bean id="userApprovalHandler" class="org.springframework.security.oauth2.provider.approval.TokenServicesUserApprovalHandler">
        <property name="tokenServices" ref="tokenServices" />
    </bean>

    <!-- authorization-server aka AuthorizationServerTokenServices is an interface that defines everything necessary for token management -->
    <oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices"
        user-approval-handler-ref="userApprovalHandler" >
        <oauth:authorization-code />
        <oauth:implicit />
        <oauth:refresh-token />
        <oauth:client-credentials />
        <oauth:password />
    </oauth:authorization-server>

    <oauth:resource-server id="resourceServerFilter" resource-id="yagger" token-services-ref="tokenServices" entry-point-ref="oauthAuthenticationEntryPoint"/>
    <!-- ClientsDeailsService: Entry Point to clients database (given is in memory implementation) -->
    <oauth:client-details-service id="clientDetails">
    <!-- client -->
    <oauth:client client-id="the_client" authorized-grant-types="authorization_code,client_credentials"
            authorities="ROLE_USER" scope="read,write,trust" secret="secret" resource-ids="yagger"/>

    <oauth:client client-id="my-trusted-client-with-secret" authorized-grant-types="password,authorization_code,refresh_token,implicit"
            secret="somesecret" authorities="ROLE_USER" scope="read,write,trust" resource-ids="yagger"/>

    </oauth:client-details-service>

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

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

</beans>

myproject-servlet.xml包含这些相关的详细信息

<beans:alias name="userOauthDetailsService" alias="userDetailsService"/>
    <beans:bean id="userOauthDetailsService" class="com.devyug.yagger.services.impl.UserOauthDetailsService" />
<sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true" secured-annotations="enabled">
        <!--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>

何时返回令牌 http://localhost:8080/YaggerWeb/oauth/token?grant_type=password&client_id=my-trusted-client-with-secret&client_secret=somesecret&username=abhiljain&password=password

但是什么时候 http://localhost:8080/YaggerWeb/yagger/sdfghjjhg?access_token=30d3b413-8691-4a8c-8f17-af0b756f3ce0 给 状态码:500内部服务器错误AuthenticationCredentialsNotFoundException:在SecurityContext中找不到Authentication对象 如果需要其他一些细节,请告诉我

0 个答案:

没有答案