com.github.jens_meiss.blog.server.configuration.BlogWebSecurityAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.github.jens_meiss.blog.server.configuration.BlogWebSecurityAdapter.java

Source

/*
 * This file is part of blog (https://github.com/jens-meiss/blog).
 *
 *  blog is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  blog 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with blog. If not, see <http://www.gnu.org/licenses/>.
 */
package com.github.jens_meiss.blog.server.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.github.jens_meiss.blog.server.service.json.user.UserController;

/**
 * The Class BlogWebSecurityAdapter.
 */
@Configuration
@EnableWebMvcSecurity
public class BlogWebSecurityAdapter extends WebMvcConfigurerAdapter {

    /**
     * The Class ApplicationSecurity.
     */
    @Configuration
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {

        /** The security. */
        @Autowired
        private SecurityProperties security;

        @Autowired
        private UserController userController;

        @Override
        protected void configure(final HttpSecurity http) throws Exception {

            http.authenticationProvider(userController);
            http.userDetailsService(userController);

            http.sessionManagement().sessionAuthenticationErrorUrl("/login");
            http.sessionManagement().invalidSessionUrl("/");
            http.sessionManagement().maximumSessions(1);

            http.authorizeRequests().antMatchers("/**").permitAll();
        }

        @Override
        public UserDetailsService userDetailsServiceBean() throws Exception {
            return userController;
        }
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

}