cz.muni.pa165.carparkapp.configuration.MySecurityConfig.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.pa165.carparkapp.configuration.MySecurityConfig.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.pa165.carparkapp.configuration;

import cz.muni.pa165.carparkapp.dto.EmployeeDTO;
import cz.muni.pa165.carparkapp.service.EmployeeService;
import cz.muni.pa165.carparkapp.serviceImpl.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 *
 * @author coldfront
 */
@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = { "cz.muni.pa165.carparkapp" })
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private AuthenticationProvider provider;

    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(provider);
        //        for(EmployeeDTO e : service.getAllEmployees())
        //        {
        //            System.out.println(e);
        //            auth.inMemoryAuthentication()
        //                    .withUser(e.getUserName()).password(e.getPassword()).roles(e.getRole());
        //        }
        //        auth.userDetailsService(userDetailsService);
        //                
        //        auth
        //                .inMemoryAuthentication()
        //                .withUser("user").password("password").roles("USER").and()
        //                .withUser("admin").password("password").roles("ADMIN");
    }

    //    @Bean
    //    @Override
    //    public AuthenticationManager authenticationManagerBean() throws Exception
    //    {
    //        return super.authenticationManagerBean();
    //    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/road.jpg", "/style.css").permitAll();

        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login?logout=true").permitAll();

        http.csrf().disable();

        http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN") // #6
                .anyRequest().authenticated().and().formLogin().loginPage("/login")
                .successHandler(new AuthenticationHandler()).failureUrl("/login?auth=fail").permitAll();

        http.exceptionHandling().accessDeniedPage("/403");
    }
}