com.pw.ism.WebSecurityConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.pw.ism.WebSecurityConfig.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 com.pw.ism;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpMethod;
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.crypto.password.PasswordEncoder;

/**
 *
 * @author Narsel
 */
@Configuration
@Profile("prod")
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(HttpMethod.POST, "/newmessage", "/addheartbeat", "/create/*")
                .permitAll().antMatchers("/", "/create", "/css/**", "/js/**", "/images/**").permitAll().anyRequest()
                .authenticated().and().csrf().ignoringAntMatchers("/newmessage", "/addheartbeat").and().formLogin()
                .loginPage("/login").permitAll().and().logout().permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select sso_id, password, state from APP_USER where sso_id=?")
                .passwordEncoder(passwordEncoder).authoritiesByUsernameQuery(
                        "SELECT app_user.sso_id, user_profile.type FROM public.app_user_user_profile, public.app_user, public.user_profile WHERE app_user.id = app_user_user_profile.user_id AND user_profile.id = app_user_user_profile.user_profile_id AND app_user.sso_id = ?");
    }

}