-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSecurityConfig.java
More file actions
62 lines (57 loc) · 2.12 KB
/
WebSecurityConfig.java
File metadata and controls
62 lines (57 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package hello;
import org.springframework.context.annotation.Bean;
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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
/**
* core spring security setting
*
* @author Fu
*
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* configure the authorization and basic spring security setting to use the form login
*
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll() // only permit access / and /home page
.anyRequest().authenticated() // other request need to authenticated
.and()
.formLogin()
.loginPage("/login")
.permitAll() // login page is allowed permit all
.and()
.logout()
.permitAll();
}
/**
* spring security will handle the username and password verification automatically
* as the InMemoryUserDetailsManager has been used.
*
* the key is to return the UserDetailsService which contains the method loadUserByUsername to retrieve the UserDetails object for
* authentication purpose
*
*/
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}