Код: Выделить всё
@RequestMapping(value = "/user", method = RequestMethod.GET)
public UserResponse user(Principal user) {
UserResponse response = new UserResponse();
response.setCode(CODE_SUCCESS);
response.setStatus(SUCCESS_STATUS);
response.setData(user);
return response;
}
Код: Выделить всё
@Configuration
@EnableWebSecurity(debug = true)
public class XYZLibrarySecurityConfig {
private final XYZLibraryAuthenticationProvider authProvider;
private final XYZLibraryConfig properties;
public XYZLibrarySecurityConfig(@Lazy XYZLibraryAuthenticationProvider authProvider, XYZLibraryConfig properties) {
this.authProvider = authProvider;
this.properties = properties;
}
@Bean
public XYZLibraryAuthenticationFilter authenticationFilter(AuthenticationManager authenticationManager) {
XYZLibraryAuthenticationFilter filter = new XYZLibraryAuthenticationFilter();
filter.setAuthenticationManager(authenticationManager);
filter.setAuthenticationSuccessHandler(authenticationSuccessHandler());
filter.setAuthenticationFailureHandler(authenticationFailureHandler());
filter.setSessionAuthenticationStrategy(authStrategy());
return filter;
}
@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(authProvider);
return authenticationManagerBuilder.build();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authenticationProvider(authProvider)
.addFilterBefore(authenticationFilter(authenticationManager(http)), UsernamePasswordAuthenticationFilter.class)
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.exceptionHandling(exceptionHandling -> exceptionHandling.authenticationEntryPoint(authenticationEntryPoint()))
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/rest/authentication/authenticate").permitAll()
.requestMatchers("/rest/authentication/user").permitAll()
.requestMatchers("/rest/authentication/forgotPassword").permitAll()
.requestMatchers("/rest/authentication/changePassword").permitAll()
.requestMatchers("/rest/authentication/passwordRules").permitAll()
.requestMatchers("/rest/authentication/helpContact").permitAll()
.requestMatchers("/rest/authentication/user-message").denyAll()
.requestMatchers("/rest/system-check/check").permitAll()
.requestMatchers("/rest/admin/**").hasAnyRole("Administrator", "XYZ Complete Editor")
.requestMatchers("/rest/**").hasAnyRole("User", "Administrator", "XYZ Limited User", "XYZ Limited Submitter", "XYZ Complete User", "XYZ Complete Editor")
.anyRequest().authenticated()
)
.formLogin(formLogin -> formLogin.permitAll())
.logout(logout -> logout
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK))
)
.headers(headers -> headers
.contentSecurityPolicy(csp -> csp
.policyDirectives("default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self'; frame-ancestors 'self'; form-action 'self';")
)
.httpStrictTransportSecurity(hsts -> hsts
.includeSubDomains(true)
.maxAgeInSeconds(31536000)
)
)
.sessionManagement(sessionManagement -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(csrf -> {
if (properties.getCsrf().getEnabled().booleanValue()) {
if (properties.getCsrf().getUseCookie().booleanValue()) {
CookieCsrfTokenRepository trep = (CookieCsrfTokenRepository) XYZLibraryCookieRepository.withHttpOnlyFalse();
trep.setCookiePath("/");
csrf.csrfTokenRepository(trep);
}
} else {
csrf.disable();
}
});
http.addFilterBefore(expiredSessionFilter(), SessionManagementFilter.class);
//http.addFilterBefore(authenticationFilter(authenticationManager(http)), UsernamePasswordAuthenticationFilter.class);
http.addFilterAfter(new RequestAuditingFilter(), BasicAuthenticationFilter.class);
return http.build();
}
Код: Выделить всё
@Override
public boolean supports(Class authentication) {
return authentication.equals(XYZLibraryAuthenticationToken.class);
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... der-issues
Мобильная версия