com.bcknds.demo.oauth2.security.AuthorizationServer.java Source code

Java tutorial

Introduction

Here is the source code for com.bcknds.demo.oauth2.security.AuthorizationServer.java

Source

/**
 * Copyright 2014 Michael Brush
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bcknds.demo.oauth2.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

import com.bcknds.demo.oauth2.service.UserService;

/**
 * This class specifies the AuthorizationServer. This is where the client applications are configured.
 * 
 * @author Michael Brush
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private ObjectPostProcessor<Object> objectPostProcessor;

    @Autowired
    private UserService userService;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        AuthenticationManager authenticationManager = new AuthenticationManagerBuilder(objectPostProcessor)
                .userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder()).and().getOrBuild();
        endpoints.authenticationManager(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // Configure an in memory authentication client with a non-expiring token. Change
        //   or remove accessTokenValiditySeconds to let the token expire.
        clients.inMemory().withClient("client-sample").authorizedGrantTypes("client_credentials")
                .authorities("ROLE_SAMPLE").scopes("read", "write").resourceIds("security").secret("secret")
                .accessTokenValiditySeconds(0)
                // Create a client for password authentication
                .and().withClient("client-password").authorizedGrantTypes("password").scopes("read", "write")
                .resourceIds("security").secret("secret").accessTokenValiditySeconds(0);
    }
}