com.github.mrstampy.gameboot.security.SecurityConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for com.github.mrstampy.gameboot.security.SecurityConfiguration.java

Source

/*
 *              ______                        ____              __ 
 *             / ____/___ _____ ___  ___     / __ )____  ____  / /_
 *            / / __/ __ `/ __ `__ \/ _ \   / __  / __ \/ __ \/ __/
 *           / /_/ / /_/ / / / / / /  __/  / /_/ / /_/ / /_/ / /_  
 *           \____/\__,_/_/ /_/ /_/\___/  /_____/\____/\____/\__/  
 *                                                 
 *                                 .-'\
 *                              .-'  `/\
 *                           .-'      `/\
 *                           \         `/\
 *                            \         `/\
 *                             \    _-   `/\       _.--.
 *                              \    _-   `/`-..--\     )
 *                               \    _-   `,','  /    ,')
 *                                `-_   -   ` -- ~   ,','
 *                                 `-              ,','
 *                                  \,--.    ____==-~
 *                                   \   \_-~\
 *                                    `_-~_.-'
 *                                     \-~
 * 
 *                       http://mrstampy.github.io/gameboot/
 *
 * Copyright (C) 2015, 2016 Burton Alexander
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * 
 */
package com.github.mrstampy.gameboot.security;

import static org.apache.commons.lang3.StringUtils.isEmpty;

import java.security.SecureRandom;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import com.github.mrstampy.gameboot.util.GameBootUtils;

/**
 * Security configuration for GameBoot.
 */
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

    /** The Constant GAME_BOOT_SECURE_RANDOM. */
    public static final String GAME_BOOT_SECURE_RANDOM = "GameBoot Secure Random";

    @Autowired
    private GameBootUtils utils;

    @Value("${gameboot.secure.random.seed.size}")
    private int seedSize;

    @Value("${secure.random.algorithm}")
    private String algorithm;

    /**
     * Post construct.
     *
     * @throws Exception
     *           the exception
     */
    @PostConstruct
    public void postConstruct() throws Exception {
        if (!utils.isPowerOf2(seedSize)) {
            throw new IllegalArgumentException(
                    "gameboot.secure.random.seed.size must be a power of 2: " + seedSize);
        }
    }

    /**
     * Secure random.
     *
     * @return the secure random
     * @throws Exception
     *           the exception
     */
    @Bean(name = GAME_BOOT_SECURE_RANDOM)
    public SecureRandom secureRandom() throws Exception {
        SecureRandom random = isEmpty(algorithm) ? SecureRandom.getInstanceStrong()
                : SecureRandom.getInstance(algorithm);

        byte[] seed = new byte[seedSize];
        random.nextBytes(seed);

        return random;
    }

}