package com.monkeyk.sos.config; import com.monkeyk.sos.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; /** * 2016/4/3 *

* Replace security.xml * * @author Shengzhao Li */ @Configuration @EnableWebSecurity public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Autowired private UserService userService; @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override public void configure(WebSecurity web) throws Exception { //Ignore, public web.ignoring().antMatchers("/public/**", "/static/**"); } @Override protected void configure(HttpSecurity http) throws Exception { // http.csrf().disable() http.authorizeRequests() .antMatchers("/public/**").permitAll() .antMatchers("/static/**").permitAll() .antMatchers("/oauth2/rest_token*").permitAll() .antMatchers("/login*").permitAll() .antMatchers("/admin/**").hasAnyRole("ADMIN") .antMatchers(HttpMethod.GET, "/login*").anonymous() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .loginProcessingUrl("/signin") .failureUrl("/login?error=1") .usernameParameter("oidc_user") .passwordParameter("oidcPwd") .and() .csrf() .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize")) .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/token")) .disable() .logout() .logoutUrl("/signout") .deleteCookies("JSESSIONID") .logoutSuccessUrl("/") .and() .exceptionHandling(); http.authenticationProvider(authenticationProvider()); } @Bean public AuthenticationProvider authenticationProvider() { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); daoAuthenticationProvider.setUserDetailsService(userService); daoAuthenticationProvider.setPasswordEncoder(passwordEncoder()); return daoAuthenticationProvider; } /** * BCrypt 加密 * * @return PasswordEncoder */ @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }