Home » Java » How to give access to my resources in SpringSecurity

How to give access to my resources in SpringSecurity

Posted by: admin June 14, 2018 Leave a comment

Questions:

I have one project which I’m working on. I have a problem with my resources. I use spring Security and I couldn’t give access to the resources I use to make my html pages. Could someone help me?
Here are my resources:
here

and here is my SpringSecurityConfig

    <!DOCTYPE html>
<html lang="en" xmlns:th="http:thymeleaf.org">

<head>
    <title>Events & People</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="./styling.css">

</head>

package com.example.app.Configuration;

import com.example.app.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
private UserService userService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(this.userService).passwordEncoder(getBCryptPasswordEncoder());

}

@Override
protected void configure(HttpSecurity http) throws Exception{
    http
            .authorizeRequests()
            .antMatchers("/","/register", "/login").permitAll()
            .antMatchers("/css/**").permitAll()
            .antMatchers("/user/**").access("hasRole('USER') OR hasRole('ADMIN')")
            .antMatchers("/admin/**").hasRole("ADMIN")
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").permitAll()
            .usernameParameter("username")
            .passwordParameter("password")
            .and()
                .rememberMe()
                .rememberMeCookieName("TheBoardFinders")
                .rememberMeParameter("remember")
                .key("1random%2316secretcryptoTUES")
                .tokenValiditySeconds(2629743)
            .and()
                .logout().logoutSuccessUrl("/login?logout").permitAll()
            .and()
                .exceptionHandling().accessDeniedPage("/unauthorized")
            .and()
                .csrf().disable();
}

@Bean
public BCryptPasswordEncoder getBCryptPasswordEncoder()
{
    return new BCryptPasswordEncoder();
}

}

Please don’t mark it as duplicate because I searched here and I found several questions like mine but they could’t help me.

Answers: