title: Access is denied in spring oauth 2
body: <p>I was looking through oAuth2.0 and I wrote this demo code, but I am not getting access token.
  <a href="http://localhost:8080/oauth/token?grant_type=password&amp;client_id=restapp2&amp;client_secret=secret&amp;username=admin&amp;password=admin" rel="nofollow noreferrer">http://localhost:8080/oauth/token?grant_type=password&amp;client_id=restapp2&amp;client_secret=secret&amp;username=admin&amp;password=admin</a></p>
  
  <p>It shows Access is Denied as</p>
  
  <pre><code>&lt;error_description&gt;Access is denied&lt;/error_description&gt;
  &lt;error&gt;access_denied&lt;/error&gt;
  </code></pre>
  
  <p>Please, help me out.</p>
  
  <p><strong>spring-security.xml</strong></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2" xmlns:context="http://www.springframework.org/schema/context" xmlns:sec="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "&gt;
  
  &lt;http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY"/&gt;
      &lt;anonymous enabled="false"/&gt;
      &lt;http-basic entry-point-ref="clientAuthenticationEntryPoint"/&gt;
  
      &lt;custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER"/&gt;
      &lt;access-denied-handler ref="oauthAccessDeniedHandler"/&gt;
  &lt;/http&gt;
  
  &lt;http pattern="/admin**" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint" access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;anonymous enabled="false"/&gt;
      &lt;intercept-url pattern="/admin**" access="ROLE_APP"/&gt;
      &lt;custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/&gt;
      &lt;access-denied-handler ref="oauthAccessDeniedHandler"/&gt;
  &lt;/http&gt;
  &lt;bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
      &lt;property name="realmName" value="test"/&gt;
  &lt;/bean&gt;
  &lt;bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
      &lt;property name="realmName" value="test/client"/&gt;
      &lt;property name="typeName" value="Basic"/&gt;
  &lt;/bean&gt;
  &lt;bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/&gt;
  &lt;bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"&gt;
      &lt;property name="authenticationManager" ref="clientAuthenticationManager"/&gt;
  &lt;/bean&gt;
  &lt;bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans"&gt;
      &lt;constructor-arg&gt;
          &lt;list&gt;
              &lt;bean class="org.springframework.security.web.access.expression.WebExpressionVoter"/&gt;
              &lt;bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/&gt;
              &lt;bean class="org.springframework.security.access.vote.RoleVoter"/&gt;
              &lt;bean class="org.springframework.security.access.vote.AuthenticatedVoter"/&gt;
          &lt;/list&gt;
      &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  &lt;authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;authentication-provider user-service-ref="clientDetailsUserService"/&gt;
  &lt;/authentication-manager&gt;
  
  &lt;authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;authentication-provider&gt;
          &lt;user-service&gt;
              &lt;user name="admin" password="admin" authorities="ROLE_APP"/&gt;
          &lt;/user-service&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  &lt;bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService"&gt;
      &lt;constructor-arg ref="clientDetails"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore"/&gt;
  
  &lt;bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"&gt;
      &lt;property name="tokenStore" ref="tokenStore"/&gt;
      &lt;property name="supportRefreshToken" value="true"/&gt;
      &lt;property name="accessTokenValiditySeconds" value="120"/&gt;
      &lt;property name="clientDetailsService" ref="clientDetails"/&gt;
  &lt;/bean&gt;
  &lt;bean id="userApprovalHandler"
        class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler"&gt;
      &lt;property name="tokenStore" ref="tokenStore"/&gt;
      &lt;property name="requestFactory" ref="requestFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="requestFactory" class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory"&gt;
      &lt;constructor-arg name="clientDetailsService" ref="clientDetails"/&gt;
  &lt;/bean&gt;
  &lt;oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices" user-approval-handler-ref="userApprovalHandler"&gt;
      &lt;oauth:authorization-code/&gt;
      &lt;oauth:implicit/&gt;
      &lt;oauth:refresh-token/&gt;
      &lt;oauth:client-credentials/&gt;
      &lt;oauth:password/&gt;
  &lt;/oauth:authorization-server&gt;
  &lt;oauth:resource-server id="resourceServerFilter" resource-id="test" token-services-ref="tokenServices"/&gt;
  &lt;oauth:client-details-service id="clientDetails"&gt;
  
      &lt;oauth:client client-id="restapp1" authorized-grant-types="authorization_code,client_credentials" authorities="ROLE_APP" scope="read,write,trust" secret="secret"/&gt;
      &lt;oauth:client client-id="restapp2" authorized-grant-types="password,authorization_code,refresh_token,implicit" secret="secret" authorities="ROLE_APP"/&gt;
  &lt;/oauth:client-details-service&gt;
  &lt;sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true"&gt;
  
      &lt;sec:expression-handler ref="oauthExpressionHandler"/&gt;
  &lt;/sec:global-method-security&gt;
  &lt;oauth:expression-handler id="oauthExpressionHandler"/&gt;
  &lt;oauth:web-expression-handler id="oauthWebExpressionHandler"/&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;WEB-INF/spring-servlet.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;WEB-INF/spring-security.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>spring-servlet.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd
                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  &lt;!--&lt;beans xmlns="http://www.springframework.org/schema/beans"--&gt;
  &lt;!--xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"--&gt;
  &lt;!--xmlns:context="http://www.springframework.org/schema/context"--&gt;
  &lt;!--xmlns:mvc="http://www.springframework.org/schema/cache"--&gt;
  &lt;!--xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"&gt;--&gt;
  &lt;context:component-scan
          base-package="com.suraj.ecommerce.controller"/&gt;&lt;!-- DispatcherServlet will search the controller class--&gt;
  &lt;context:annotation-config/&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  &lt;mvc:resources mapping="/resources/**" location="/resources/assets"/&gt;
  &lt;mvc:annotation-driven/&gt;&lt;!--for using the map--&gt;
  
  &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/pages/"/&gt;
      &lt;property name="suffix" value=".jsp"/&gt;
  &lt;/bean&gt;
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/ecommerce"/&gt;
      &lt;property name="username" value="root"/&gt;
      &lt;property name="password" value=""/&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt;
      &lt;property name="host" value="smtp.gmail.com" /&gt;
      &lt;property name="port" value="587" /&gt;
      &lt;property name="username" value="abc@gmail.com " /&gt;
      &lt;property name="password" value="abc" /&gt;
  
      &lt;property name="javaMailProperties"&gt;
          &lt;props&gt;
              &lt;prop key="mail.smtp.auth"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.smtp.starttls.enable"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="mail" class="com.suraj.ecommerce.others.Mail"/&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="packagesToScan" value="com.suraj.ecommerce.entity"/&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!--&lt;bean id="signupService" class="com.suraj.ecommerce.service.SignUpService"/&gt;--&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="productDAOImpl" class="com.suraj.ecommerce.daoimpl.ProductDAOImpl"/&gt;
  &lt;bean id="productService" class="com.suraj.ecommerce.service.ProductService"/&gt;
  &lt;bean id="signupService" class="com.suraj.ecommerce.service.SignUpService"/&gt;
  &lt;bean id="signupDAOImpl" class="com.suraj.ecommerce.daoimpl.SignUpDAOImpl"/&gt;
  </code></pre>
  
  <p></p>