/
CredentialsAndJwtSecurityConfig.java
82 lines (66 loc) · 3.22 KB
/
CredentialsAndJwtSecurityConfig.java
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
76
77
78
79
80
81
82
package us.careydevelopment.ecosystem.jwt.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter;
import us.careydevelopment.ecosystem.jwt.model.IpTracker;
import us.careydevelopment.ecosystem.jwt.service.JwtUserDetailsService;
/**
* This class gets extended in the Spring Boot application
* Handles security config for applications that support user login and JWT
*/
public abstract class CredentialsAndJwtSecurityConfig extends BaseSecurityConfig {
protected JwtUserDetailsService jwtUserDetailsService;
protected IpTracker ipTracker;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
auth.userDetailsService(jwtUserDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* Yep, we're using BearerTokenAuthenticationFilter even though it's designed for OAuth2.
* That's because it's scalable - we'll probably go to OAuth2 one day.
* Plus it works. So why complain?
*
* @return bearerTokenAuthenticationFilter
* @throws Exception
*/
protected BearerTokenAuthenticationFilter bearerTokenAuthenticationFilter() throws Exception {
BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(authenticationManager());
filter.setAuthenticationFailureHandler(authenticationFailureHandler());
return filter;
}
/**
* This filter handles name/password authentication
*
* @return credentialsAuthenticationFilter
* @throws Exception
*/
protected CredentialsAuthenticationFilter credentialsAuthenticationFilter() throws Exception {
CredentialsAuthenticationFilter filter = new CredentialsAuthenticationFilter(authenticationManager());
filter.setAuthenticationFailureHandler(authenticationFailureHandler());
filter.setJwtTokenUtil(jwtUtil);
filter.setUserDetailsService(jwtUserDetailsService);
filter.setIpTracker(ipTracker);
return filter;
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors().and()
.csrf().disable()
.addFilter(bearerTokenAuthenticationFilter())
.addFilter(credentialsAuthenticationFilter())
.authorizeRequests()
.anyRequest().hasAnyAuthority(allowedAuthorities).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}