Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After a few days of struggling I ended up with a working configuration. Since there is a shortage of good working example on the internet, I will share mine here</p> <pre class="lang-xml prettyprint-override"><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:jee="http://www.springframework.org/schema/jee" xmlns:sec="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"&gt; &lt;!-- &lt;sec:debug /&gt; --&gt; &lt;!-- Used by the token store --&gt; &lt;bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt; &lt;property name="url" value="${jdbc.url}" /&gt; &lt;property name="username" value="${jdbc.username}" /&gt; &lt;property name="password" value="${jdbc.password}" /&gt; &lt;/bean&gt; &lt;!-- Server configuration --&gt; &lt;oauth:authorization-server client-details-service-ref="clientDetailsService" 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;bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener" /&gt; &lt;!-- Services for clients --&gt; &lt;sec:authentication-manager id="clientAuthenticationManager"&gt; &lt;sec:authentication-provider user-service-ref="clientDetailsUserService" /&gt; &lt;/sec:authentication-manager&gt; &lt;oauth:client-details-service id="clientDetailsService"&gt; &lt;oauth:client client-id="client1" authorized-grant-types="client_credentials,password,implicit" authorities="ROLE_WRITE" secret="secret" /&gt; &lt;/oauth:client-details-service&gt; &lt;bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService"&gt; &lt;constructor-arg ref="clientDetailsService" /&gt; &lt;/bean&gt; &lt;!-- service for resolving our users. --&gt; &lt;authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security"&gt; &lt;authentication-provider user-service-ref="userService" /&gt; &lt;/authentication-manager&gt; &lt;bean id="userService" class="our.UserServiceImpl" /&gt; &lt;!-- Managing Tokens --&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="clientDetailsService" ref="clientDetailsService" /&gt; &lt;property name="accessTokenValiditySeconds" value="${security.token.validitySeconds:43200}" /&gt; &lt;/bean&gt; &lt;bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.JdbcTokenStore"&gt; &lt;constructor-arg ref="mysqlDataSource" /&gt; &lt;/bean&gt; &lt;!-- Token Approval Handler --&gt; &lt;bean id="userApprovalHandler" class="org.springframework.security.oauth2.provider.approval.TokenServicesUserApprovalHandler"&gt; &lt;property name="tokenServices" ref="tokenServices" /&gt; &lt;/bean&gt; &lt;!-- Resource server --&gt; &lt;oauth:resource-server id="resourceServerFilter" resource-id="myRealm" token-services-ref="tokenServices" /&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" before="BASIC_AUTH_FILTER" /&gt; &lt;access-denied-handler ref="oauthAccessDeniedHandler" /&gt; &lt;/http&gt; &lt;http pattern="/oauth/authorize/**" access-denied-page="/login.jsp?authorization_error=true" disable-url-rewriting="true" xmlns="http://www.springframework.org/schema/security"&gt; &lt;intercept-url pattern="/oauth/authorize/**" access="IS_AUTHENTICATED_FULLY" /&gt; &lt;form-login authentication-failure-url="/login.jsp?authentication_error=true" default-target-url="http://www.ourwebsite.com/" login-page="/login.jsp" login-processing-url="/login.do" /&gt; &lt;http-basic /&gt; &lt;anonymous /&gt; &lt;/http&gt; &lt;http pattern="/login**" access-denied-page="/login.jsp?authorization_error=true" disable-url-rewriting="true" xmlns="http://www.springframework.org/schema/security"&gt; &lt;intercept-url pattern="/login**" access="IS_AUTHENTICATED_ANONYMOUSLY" /&gt; &lt;form-login authentication-failure-url="/login.jsp?authentication_error=true" default-target-url="http://www.outwebsite.com" login-page="/login.jsp" login-processing-url="/login.do" /&gt; &lt;http-basic /&gt; &lt;/http&gt; &lt;http pattern="/**" create-session="stateless" entry-point-ref="clientAuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/security" access-decision-manager-ref="accessDecisionManager"&gt; &lt;intercept-url pattern="/**" access="ROLE_WRITE" /&gt; &lt;custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /&gt; &lt;access-denied-handler ref="oauthAccessDeniedHandler" /&gt; &lt;/http&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.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;bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt; &lt;property name="realmName" value="myRealm" /&gt; &lt;/bean&gt; &lt;bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /&gt; </code></pre> <p></p> <p>My <code>web.xml</code> looks like this:</p> <pre class="lang-xml prettyprint-override"><code>&lt;web-app id="Recipe_REST_API" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt; &lt;display-name&gt;Our REST API&lt;/display-name&gt; &lt;!-- Servlets --&gt; &lt;servlet&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;mvc-dispatcher&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/mvc-dispatcher-servlet.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- filters --&gt; &lt;filter&gt; &lt;filter-name&gt;httpMethodFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.HiddenHttpMethodFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;httpMethodFilter&lt;/filter-name&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;/filter-mapping&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;!-- listeners --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload