-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
75 lines (69 loc) · 3.39 KB
/
SecurityConfig.java
File metadata and controls
75 lines (69 loc) · 3.39 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
63
64
65
66
67
68
69
70
71
72
73
74
75
package guru.sfg.brewery.config;
import guru.sfg.brewery.filter.RestHeaderAuthFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
public RestHeaderAuthFilter restHeaderAuthFilter(AuthenticationManager authenticationManager){
RestHeaderAuthFilter restHeaderAuthFilter = new RestHeaderAuthFilter(new AntPathRequestMatcher("/api/**"));
restHeaderAuthFilter.setAuthenticationManager(authenticationManager);
return restHeaderAuthFilter;
}
// By default, Spring Security turns on csrf protection.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(restHeaderAuthFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class)
.csrf().disable();
http
.authorizeRequests(authorize -> {
authorize
.antMatchers("/", "/webjars/**", "/login", "/resources/**").permitAll()
.antMatchers("/beers/find", "/beers*").permitAll()
.antMatchers(HttpMethod.GET, "/api/v1/beer/**").permitAll()
.mvcMatchers(HttpMethod.GET, "/api/v1/beerUpc/{upc}").permitAll();
} )
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}
// Configuring In Memory Authentication using Authentication Fluent API.
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("spring")
.password("guru")
.roles()
.and()
.withUser("user")
.password("{noop}password")
.roles("USER");
}
// This is kinda an old way of creating InMemory UserDetails Service. Alternatively, we can use
// In Memory Authentication Fluent API. See above
// @Override
// @Bean
// protected UserDetailsService userDetailsService() {
// UserDetails admin = User.withDefaultPasswordEncoder()
// .username("spring")
// .password("guru")
// .roles("ADMIN")
// .build();
// UserDetails user = User.withDefaultPasswordEncoder()
// .username("user")
// .password("password")
// .roles("USER")
// .build();
// // InMemoryUserDetailsManager implements UserDetailsService thus overriding default UserDetailsService
// return new InMemoryUserDetailsManager(admin, user);
// }
}