Project

General

Profile

« Previous | Next » 

Revision 01fe562b

Added by Andreas Kohlbecker over 7 years ago

ref #6118 OAuth2 AuthorizationServer implemented

View differences:

cdmlib-remote-webapp/src/main/webapp/WEB-INF/applicationContext.xml
56 56
  <!-- CONFIGURE WEB APPLICATION HERE -->
57 57
  <import resource="datasources/configurable.xml" />
58 58
  
59
  <!-- bootstrap the WebSecurityConfiguration -->
60
  <bean class="eu.etaxonomy.cdm.remote.config.MultiWebSecurityConfiguration"></bean>
61 59

  
62 60
  <bean class="eu.etaxonomy.cdm.remote.config.LoggingConfigurer">
63 61
  </bean>
64 62

  
63
  <!-- bootstrap the WebSecurityConfiguration -->
64
  <bean class="eu.etaxonomy.cdm.remote.config.MultiWebSecurityConfiguration">
65
  </bean>
66
  <bean class="eu.etaxonomy.cdm.remote.config.AuthorizationServerConfiguration">
67
  </bean>
68

  
65 69
  <!-- OAI-PMH TODO externalize? -->
66 70
  <bean name="taxonOaiPmhController"
67 71
    class="eu.etaxonomy.cdm.remote.controller.oaipmh.TaxonOaiPmhController">
cdmlib-remote/.springBeans
1 1
<?xml version="1.0" encoding="UTF-8"?>
2 2
<beansProjectDescription>
3 3
	<version>1</version>
4
	<pluginVersion><![CDATA[3.4.0.201310051539-RELEASE]]></pluginVersion>
4
	<pluginVersion><![CDATA[3.6.3.201411271034-RELEASE]]></pluginVersion>
5 5
	<configSuffixes>
6 6
		<configSuffix><![CDATA[xml]]></configSuffix>
7 7
	</configSuffixes>
8 8
	<enableImports><![CDATA[false]]></enableImports>
9 9
	<configs>
10
		<config>src/main/resources/eu/etaxonomy/cdm/remote-security.xml</config>
11 10
	</configs>
11
	<autoconfigs>
12
	</autoconfigs>
12 13
	<configSets>
13 14
	</configSets>
14 15
</beansProjectDescription>
cdmlib-remote/pom.xml
60 60
        <groupId>org.springframework</groupId>
61 61
        <artifactId>spring-webmvc</artifactId>
62 62
    </dependency>
63
    
64 63
    <dependency>
65 64
      <groupId>org.springmodules</groupId>
66 65
      <artifactId>spring-modules-cache</artifactId>
......
79 78
    <dependency>
80 79
      <groupId>org.springframework.security</groupId>
81 80
      <artifactId>spring-security-core</artifactId>
82
      <version>${spring-security.version}</version>
83 81
    </dependency>
84 82
    <dependency>
85 83
      <groupId>org.springframework.security</groupId>
86 84
      <artifactId>spring-security-config</artifactId>
87
      <version>${spring-security.version}</version>
88 85
    </dependency>
89 86
    <dependency>
90 87
      <groupId>org.springframework.security</groupId>
91 88
      <artifactId>spring-security-web</artifactId>
92
      <version>${spring-security.version}</version>
89
    </dependency>
90
    <dependency>
91
      <groupId>org.springframework.security.oauth</groupId>
92
      <artifactId>spring-security-oauth2</artifactId>
93 93
    </dependency>
94 94
<!--     <dependency> -->
95 95
<!--       <groupId>org.unitils</groupId> -->
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/AuthorizationServerConfiguration.java
1
/**
2
* Copyright (C) 2016 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.config;
10

  
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.context.annotation.Bean;
13
import org.springframework.context.annotation.Configuration;
14
import org.springframework.context.annotation.Lazy;
15
import org.springframework.context.annotation.Scope;
16
import org.springframework.context.annotation.ScopedProxyMode;
17
import org.springframework.security.authentication.AuthenticationManager;
18
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
19
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
20
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
21
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
22
import org.springframework.security.oauth2.provider.ClientDetailsService;
23
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
24
import org.springframework.security.oauth2.provider.approval.InMemoryApprovalStore;
25
import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
26
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
27
import org.springframework.security.oauth2.provider.token.TokenStore;
28
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
29

  
30
import eu.etaxonomy.cdm.remote.oauth2.CdmUserApprovalHandler;
31

  
32
/**
33
 * @author a.kohlbecker
34
 * @date Oct 6, 2016
35
 *
36
 */
37
@Configuration
38
@EnableAuthorizationServer
39
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
40

  
41
    /**
42
     *
43
     */
44
    private static final String RESOURCE_ID = "all-services";
45
    /**
46
     *
47
     */
48
    private static final String CLIENT_ID = "any-client";
49

  
50
    @Autowired
51
    private ClientDetailsService clientDetailsService;
52

  
53
    @Autowired
54
    private UserApprovalHandler userApprovalHandler;
55

  
56
    @Autowired
57
    private AuthenticationManager authenticationManager;
58

  
59

  
60
    @Override
61
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
62
        clients.inMemory().withClient(CLIENT_ID)
63
            .resourceIds(RESOURCE_ID)
64
            .authorizedGrantTypes("implicit")
65
            .authorities("ROLE_USER")
66
            .scopes("read", "write")
67
            .autoApprove("read");
68
    }
69

  
70
    @Bean
71
    public ApprovalStore approvalStore() {
72
        return new InMemoryApprovalStore();
73
    }
74

  
75
    @Bean
76
    public TokenStore tokenStore() {
77
        return new InMemoryTokenStore();
78
    }
79

  
80
    @Override
81
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
82
        endpoints.tokenStore(tokenStore()).userApprovalHandler(userApprovalHandler)
83
        .authenticationManager(authenticationManager);
84
    }
85

  
86
    @Bean
87
    @Lazy
88
    @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
89
    public CdmUserApprovalHandler userApprovalHandler() throws Exception {
90
        CdmUserApprovalHandler handler = new CdmUserApprovalHandler();
91
        handler.setApprovalStore(approvalStore());
92
        handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
93
        handler.setClientDetailsService(clientDetailsService);
94
        handler.setUseApprovalStore(false);
95
        return handler;
96
    }
97
}
98

  
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/oauth2/CdmUserApprovalHandler.java
1
/*
2
 * This class has been taken from org.springframework.security.oauth.examples.sparklr
3
 *
4
 * ----------------------------------------------------------------------------------
5
 *
6
 * Copyright 2002-2011 the original author or authors.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *      http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
package eu.etaxonomy.cdm.remote.oauth2;
21

  
22
import java.util.Collection;
23

  
24
import org.springframework.security.core.Authentication;
25
import org.springframework.security.oauth2.provider.AuthorizationRequest;
26
import org.springframework.security.oauth2.provider.ClientDetails;
27
import org.springframework.security.oauth2.provider.ClientDetailsService;
28
import org.springframework.security.oauth2.provider.ClientRegistrationException;
29
import org.springframework.security.oauth2.provider.approval.ApprovalStoreUserApprovalHandler;
30

  
31
/**
32
 * @author a.kohlbecker
33
 * @date Oct 6, 2016
34
 *
35
 */
36
public class CdmUserApprovalHandler extends ApprovalStoreUserApprovalHandler {
37

  
38
    private boolean useApprovalStore = true;
39

  
40
    private ClientDetailsService clientDetailsService;
41

  
42
    /**
43
     * Service to load client details (optional) for auto approval checks.
44
     *
45
     * @param clientDetailsService a client details service
46
     */
47
    @Override
48
    public void setClientDetailsService(ClientDetailsService clientDetailsService) {
49
        this.clientDetailsService = clientDetailsService;
50
        super.setClientDetailsService(clientDetailsService);
51
    }
52

  
53
    /**
54
     * @param useApprovalStore the useTokenServices to set
55
     */
56
    public void setUseApprovalStore(boolean useApprovalStore) {
57
        this.useApprovalStore = useApprovalStore;
58
    }
59

  
60
    /**
61
     * Allows automatic approval for a white list of clients in the implicit grant case.
62
     *
63
     * @param authorizationRequest The authorization request.
64
     * @param userAuthentication the current user authentication
65
     *
66
     * @return An updated request if it has already been approved by the current user.
67
     */
68
    @Override
69
    public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest,
70
            Authentication userAuthentication) {
71

  
72
        boolean approved = false;
73
        // If we are allowed to check existing approvals this will short circuit the decision
74
        if (useApprovalStore) {
75
            authorizationRequest = super.checkForPreApproval(authorizationRequest, userAuthentication);
76
            approved = authorizationRequest.isApproved();
77
        }
78
        else {
79
            if (clientDetailsService != null) {
80
                Collection<String> requestedScopes = authorizationRequest.getScope();
81
                try {
82
                    ClientDetails client = clientDetailsService
83
                            .loadClientByClientId(authorizationRequest.getClientId());
84
                    for (String scope : requestedScopes) {
85
                        if (client.isAutoApprove(scope)) {
86
                            approved = true;
87
                            break;
88
                        }
89
                    }
90
                }
91
                catch (ClientRegistrationException e) {
92
                }
93
            }
94
        }
95
        authorizationRequest.setApproved(approved);
96

  
97
        return authorizationRequest;
98

  
99
    }
100

  
101
}
pom.xml
27 27
    <java.codelevel>1.7</java.codelevel>
28 28
    <spring.version>4.2.4.RELEASE</spring.version>
29 29
    <spring-security.version>4.0.3.RELEASE</spring-security.version>
30
    <spring-security-oauth2.version>2.0.11.RELEASE</spring-security-oauth2.version>
31
    <spring-cloud.version>1.1.3.RELEASE</spring-cloud.version>
30 32
	<hibernate.version>5.0.7.Final</hibernate.version>
31 33
	<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
32 34
    <hibernate-search.version>5.5.2.Final</hibernate-search.version>
......
1374 1376
        <artifactId>spring-security-web</artifactId>
1375 1377
        <version>${spring-security.version}</version>
1376 1378
      </dependency>
1379
      <dependency>
1380
        <groupId>org.springframework.security.oauth</groupId>
1381
      <artifactId>spring-security-oauth2</artifactId>
1382
        <version>${spring-security-oauth2.version}</version>
1383
      </dependency>
1384
      <dependency>
1385
        <groupId>org.springframework.cloud</groupId>
1386
        <artifactId>spring-cloud-security</artifactId>
1387
        <version>${spring-security.version}</version>
1388
      </dependency>
1377 1389

  
1378 1390
      <!-- ******* SERVLET/JSP/JSF ******* -->
1379 1391
      <dependency>

Also available in: Unified diff