- Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
Latest commit
143 lines (118 loc) · 5.45 KB
/
Copy pathSecurityConfig.java
File metadata and controls
143 lines (118 loc) · 5.45 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
packagecom.github.tutorial.Spring;
importcom.github.tutorial.Security.CustomAccessDeniedHandler;
importcom.github.tutorial.Security.CustomLogoutSuccessHandler;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
importorg.springframework.security.config.annotation.web.builders.HttpSecurity;
importorg.springframework.security.config.annotation.web.builders.WebSecurity;
importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
importorg.springframework.security.config.http.SessionCreationPolicy;
importorg.springframework.security.core.session.SessionRegistry;
importorg.springframework.security.core.session.SessionRegistryImpl;
importorg.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
importorg.springframework.security.web.AuthenticationEntryPoint;
importorg.springframework.security.web.access.AccessDeniedHandler;
importorg.springframework.security.web.authentication.AuthenticationFailureHandler;
importorg.springframework.security.web.authentication.AuthenticationSuccessHandler;
importorg.springframework.security.web.authentication.logout.LogoutSuccessHandler;
importorg.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
importorg.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
importorg.springframework.security.web.util.matcher.AntPathRequestMatcher;
importjavax.servlet.http.HttpServletResponse;
importjavax.sql.DataSource;
@Configuration
@EnableWebSecurity
publicclassSecurityConfigextendsWebSecurityConfigurerAdapter {
@Autowired
privateBCryptPasswordEncoderbCryptPasswordEncoder;
@Autowired
privateAuthenticationSuccessHandlersuccessHandler;
@Autowired
privateAuthenticationFailureHandlerauthenticationFailureHandler;
@Autowired
privateDataSourcedataSource;
@Value("${spring.queries.users-query}")
privateStringusersQuery;
@Value("${spring.queries.roles-query}")
privateStringrolesQuery;
publicSecurityConfig() {
super();
}
@Override
protectedvoidconfigure(AuthenticationManagerBuilderauth)
throwsException {
auth.
jdbcAuthentication()
.usersByUsernameQuery(usersQuery)
.authoritiesByUsernameQuery(rolesQuery)
.dataSource(dataSource)
.passwordEncoder(bCryptPasswordEncoder);
}
@Override
protectedvoidconfigure(HttpSecurityhttp) throwsException {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/registration").permitAll()
.antMatchers("/registrations").permitAll()
.antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
.authenticated().and().csrf().disable()
.formLogin()
.loginPage("/login").failureUrl("/login?error=true")
.defaultSuccessUrl("/admin")
.usernameParameter("email")
.passwordParameter("password")
.successHandler(successHandler)
.failureHandler(authenticationFailureHandler)
.and()
.logout()
.logoutRequestMatcher(newAntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/logout").deleteCookies("JSESSIONID").deleteCookies("my-rememberme")
.logoutSuccessHandler(logoutSuccessHandler())
.and().rememberMe()
.tokenRepository(persistentTokenRepository())
.and()
// .exceptionHandling().accessDeniedHandler(accessDeniedHandler())
//.and()
.headers().cacheControl().disable()
.and().sessionManagement()
.sessionFixation().migrateSession()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.invalidSessionUrl("/invalidSession")
.maximumSessions(1)
.expiredUrl("/invalidSession");
}
@Bean
publicPersistentTokenRepositorypersistentTokenRepository() {
JdbcTokenRepositoryImpltokenRepositoryImpl = newJdbcTokenRepositoryImpl();
tokenRepositoryImpl.setDataSource(dataSource);
returntokenRepositoryImpl;
}
@Bean
publicLogoutSuccessHandlerlogoutSuccessHandler() {
returnnewCustomLogoutSuccessHandler();
}
@Bean
publicAccessDeniedHandleraccessDeniedHandler() {
returnnewCustomAccessDeniedHandler();
}
@Bean
publicAuthenticationEntryPointunauthorizedEntryPoint() {
return (request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
@Override
publicvoidconfigure(WebSecurityweb) throwsException {
web
.ignoring()
.antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**", "/webjars/**");
}
@Bean
publicSessionRegistrysessionRegistry() {
returnnewSessionRegistryImpl();
}
}