Created
September 6, 2015 05:18
-
-
Save fonlang/77c36198ebef6307a002 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | |
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | |
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; | |
import org.springframework.security.core.GrantedAuthority; | |
import org.springframework.security.core.authority.GrantedAuthorityImpl; | |
import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
import org.springframework.security.core.userdetails.User; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import org.springframework.security.core.userdetails.UserDetailsService; | |
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |
import org.springframework.security.crypto.password.PasswordEncoder; | |
import org.springframework.security.provisioning.JdbcUserDetailsManager; | |
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | |
import javax.sql.DataSource; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by justin on 5/15/14. | |
*/ | |
@Configuration | |
@EnableWebMvcSecurity | |
@EnableGlobalMethodSecurity(prePostEnabled = true) | |
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { | |
@Autowired | |
private DataSource datasource; | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http | |
.authorizeRequests().anyRequest().authenticated(); | |
http | |
.formLogin().failureUrl("/login?error") | |
.defaultSuccessUrl("/") | |
.loginPage("/login") | |
.permitAll() | |
.and() | |
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login") | |
.permitAll(); | |
} | |
@Override | |
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
JdbcUserDetailsManager userDetailsService = new JdbcUserDetailsManager(); | |
userDetailsService.setDataSource(datasource); | |
PasswordEncoder encoder = new BCryptPasswordEncoder(); | |
auth.userDetailsService(userDetailsService).passwordEncoder(encoder); | |
auth.jdbcAuthentication().dataSource(datasource); | |
if(!userDetailsService.userExists("user")) { | |
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); | |
authorities.add(new SimpleGrantedAuthority("USER")); | |
User userDetails = new User("user", encoder.encode("password"), authorities); | |
userDetailsService.createUser(userDetails); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment