1
1
.
.
1
1
.
.
9
9
S
S
e
e
c
c
u
u
r
r
i
i
t
t
y
y
-
-
W
W
e
e
b
b
S
S
e
e
c
c
u
u
r
r
i
i
t
t
y
y
C
C
o
o
n
n
f
f
i
i
g
g
.
.
j
j
a
a
v
v
a
a
I
I
n
n
f
f
o
o
Class WebSecurityConfig.java is going to be a central point in specifying different settings related to Security.\
So here is a short overview of different things that can be configured.
WebSecurityConfig.java
package com.ivoronline.springboot_security_loginform_custom.config;
import org.springframework.context.annotation.Configuration;
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
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) //Enables @Secured & @PreAuthorize
public class WebSecurityConfig extends WebSecurityConfigurerAdapt er {
private final UserDetailsService userDetailsService;
@Bean
PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
//====================================================================
// CONFIGURE
//====================================================================
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
//SPECIFY ACCESS TO ENDPOINTS
httpSecurity.authorizeRequests().antMatchers("/Authenticate").permitAll(); //Anonymouse Access (no Login)
httpSecurity.authorizeRequests().antMatchers("/Hello").hasRole("USER"); //Authenticated Access
httpSecurity.authorizeRequests().anyRequest().authenticated(); //Authenticated Access
//DEFAULT LOGIN FORM
httpSecurity.formLogin();
//CUSTOM LOGIN FORM
httpSecurity.formLogin()
.loginPage("/MyLogin")
.loginProcessingUrl("/login");
//ENABLE REMEMBER ME COOKIE
httpSecurity.rememberMe().key("something").userDetailsService(userDetailsService);
//DISABLE CSRF
httpSecurity.csrf().disable();
}
}
MyController.java
@Secured ({"ROLE_ADMIN", "ROLE_USER"})
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")