de.pksoftware.springstrap.basic.config.BasicWebSecurityConfig.java Source code

Java tutorial

Introduction

Here is the source code for de.pksoftware.springstrap.basic.config.BasicWebSecurityConfig.java

Source

/*
 * 
 * Springstrap
 *
 * @author Jan Philipp Knller <info@pksoftware.de>
 * 
 * Homepage: http://ui5strap.com/springstrap
 *
 * Copyright (c) 2013-2014 Jan Philipp Knller <info@pksoftware.de>
 * 
 * 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.
 * Released under Apache2 license: http://www.apache.org/licenses/LICENSE-2.0.txt
 * 
 */
package de.pksoftware.springstrap.basic.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import de.pksoftware.springstrap.core.config.SpringstrapConfiguration;
import de.pksoftware.springstrap.core.config.WebSecurityConfigBase;
import de.pksoftware.springstrap.core.domain.SimpleGoogleLoginAuthenticationFilter;

@Configuration
public class BasicWebSecurityConfig extends WebSecurityConfigBase {

    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * Configure the Authentication Manager for the example application. Set the UserDetailsService and a password encoder.
     */
    @Autowired
    @Override
    public void configureAuthenticationManager(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    /**
     * Configure the Web Security for the example application.
     */
    @Override
    protected void configureSecurity(HttpSecurity http) throws Exception {

        this.addAuthenticationProcessingFilter(http, googleAuthenticationProcessingFilterBean());

        //Access Rules
        String[] filesAdmin = { "/admin/**" };

        String[] filesAuthorized = { "/account/**" };

        //Authorize Requests
        http.authorizeRequests()

                .antMatchers(filesAdmin).hasAnyRole("ADMIN").antMatchers(filesAuthorized).authenticated()

                .anyRequest().permitAll();
    }

    /**
     * Expose the Google Login Filter as Bean.
     * @return
     * @throws Exception
     */
    @Bean
    protected AbstractAuthenticationProcessingFilter googleAuthenticationProcessingFilterBean() throws Exception {
        //Google Login Authentication Filter
        SimpleGoogleLoginAuthenticationFilter googleLoginAuthenticationFilter = new SimpleGoogleLoginAuthenticationFilter(
                SpringstrapConfiguration.DEFAULT_LOGIN_GOOGLE_PAGE);
        googleLoginAuthenticationFilter.setAuthenticationManager(authenticationManagerBean());

        return googleLoginAuthenticationFilter;
    }
}