com.aplikasi.penjualan.config.KonfigurasiSecurity.java Source code

Java tutorial

Introduction

Here is the source code for com.aplikasi.penjualan.config.KonfigurasiSecurity.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.aplikasi.penjualan.config;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;

/**
 *
 * @author USER
 */
@Configuration
@EnableWebMvcSecurity
public class KonfigurasiSecurity extends WebSecurityConfigurerAdapter {

    private static final String SQL_LOGIN = "select username, password, active as enabled "
            + "from s_users where username = ?";

    private static final String SQL_PERMISSION = "select u.username, r.nama as authority "
            + "from s_users u join s_user_role ur on u.id = ur.id_user " + "join s_roles r on ur.id_role = r.id "
            + "where u.username = ?";

    @Autowired
    private DataSource ds;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(ds).usersByUsernameQuery(SQL_LOGIN)
                .authoritiesByUsernameQuery(SQL_PERMISSION);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/css/**").permitAll().antMatchers("/js/**").permitAll()
                .antMatchers("/halo").hasAnyRole("ADMIN", "STAFF", "MANAGER", "ENGINEER")
                .antMatchers("/karyawan/form").hasAnyRole("ADMIN", "STAFF").antMatchers("/karyawan/list")
                .hasAnyRole("ADMIN", "STAFF", "MANAGER", "ENGINEER").antMatchers("/karyawan/view")
                .hasAnyRole("ADMIN", "STAFF", "MANAGER", "ENGINEER").anyRequest().authenticated().and().formLogin()
                .loginPage("/login").permitAll().defaultSuccessUrl("/halo").and().logout().and()
                .addFilterAfter(new CsrfAttributeToCookieFilter(), CsrfFilter.class).csrf()
                .csrfTokenRepository(csrfTokenRepository());
    }

    private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }
}