com.icm.taskmanager.web.security.SpringSecurityConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.icm.taskmanager.web.security.SpringSecurityConfig.java

Source

/*
 * =============================================================================
 *
 *   Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.org)
 *
 *   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.icm.taskmanager.web.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    public SpringSecurityConfig() {
        super();
    }

    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/taskmanager?autoReconnect=true&useSSL=false");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("password");
        return driverManagerDataSource;
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login.html").successForwardUrl("/loginok.html").failureUrl("/login-error.html")
                .and().logout().logoutSuccessUrl("/").and().authorizeRequests().antMatchers("/manager/**")
                .hasRole("MANAGER").antMatchers("/employee/**").hasRole("EMPLOYEE").and().rememberMe()
                .key("rem-me-key").rememberMeParameter("remember-me-param").rememberMeCookieName("my-remember-me")
                .tokenValiditySeconds(86400);

    }

    @Override
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource()).usersByUsernameQuery(
                "SELECT u.login, s.password, true FROM users u JOIN securities s ON s.user_id=u.user_id WHERE u.login=?")
                .authoritiesByUsernameQuery("SELECT u.login, u.authority FROM users u WHERE u.login=?");
    }

}