2024-07-08 04:12:21 +03:00

81 lines
3.7 KiB
Java

package com.alterdekim.javabot.security;
import com.alterdekim.javabot.handler.CustomAccessDeniedHandler;
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.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
public class SpringSecurity {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
requestCache.setMatchingRequestParameterName(null);
http.csrf().disable()
.authorizeHttpRequests((authorize) ->
authorize
.requestMatchers("/panel").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/api/**").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/public/**").permitAll()
.requestMatchers("/static/**").permitAll()
.requestMatchers("/access-denied").permitAll()
.requestMatchers("/signup").permitAll()
.requestMatchers("/favicon.ico").permitAll()
.requestMatchers("/signup/**").permitAll()
.requestMatchers("/").permitAll()
.requestMatchers("/script-editor").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/editor-public").permitAll()
).formLogin(
form -> form
.loginPage("/login")
.loginProcessingUrl("/login")
.failureForwardUrl("/")
.defaultSuccessUrl("/panel")
.permitAll()
)
.logout(
logout -> logout
.logoutUrl("/logout")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.permitAll()
)
.requestCache((cache) -> cache
.requestCache(requestCache)
)
.exceptionHandling((exc) -> exc
.accessDeniedHandler(accessDeniedHandler())
.accessDeniedPage("/access-denied"));
return http.build();
}
@Bean
public AccessDeniedHandler accessDeniedHandler() {
return new CustomAccessDeniedHandler();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public static PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}