Skip to content

Instantly share code, notes, and snippets.

@geine
Created June 18, 2013 00:24
Show Gist options
  • Save geine/570be44d6bdac5e35f42 to your computer and use it in GitHub Desktop.
Save geine/570be44d6bdac5e35f42 to your computer and use it in GitHub Desktop.
SecurityConfiguration
package com.blogspot.sharingebook.spring.zk;
import com.blogspot.sharingebook.spring.zk.security.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authentication.dao.ReflectionSaltSource;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.security.config.annotation.authentication.AuthenticationRegistry;
import org.springframework.security.config.annotation.web.EnableWebSecurity;
import org.springframework.security.config.annotation.web.HttpConfiguration;
import org.springframework.security.config.annotation.web.WebSecurityBuilder;
import org.springframework.security.config.annotation.web.WebSecurityConfigurerAdapter;
import javax.sql.DataSource;
/**
* Created with IntelliJ IDEA.
* User: altaire
* Date: 6/16/13
* Time: 7:40 PM
* To change this template use File | Settings | File Templates.
*/
@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = {"com.blogspot.sharingebook.spring.zk"})
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Autowired
private SecurityService securityService;
@Override
public void configure(WebSecurityBuilder builder) throws Exception {
builder
.ignoring()
.antMatchers("/resources/**");
}
@Override
protected void registerAuthentication(AuthenticationRegistry registry) throws Exception {
ShaPasswordEncoder encoder = passwordEncoder();
registry
.add(authenticationProvider())
.jdbcUserDetailsManager()
.dataSource(dataSource)
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN").and()
.usersByUsernameQuery("select username,password,true from User where username=?")
.authoritiesByUsernameQuery("select username, 'USER' from User where username=?")
.withDefaultSchema();
}
@Override
protected void configure(HttpConfiguration httpConfiguration) throws Exception {
httpConfiguration
.authorizeUrls()
.antMatchers("/users**", "/sessions/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.rememberMe();
}
@Bean
public ShaPasswordEncoder passwordEncoder() {
return new ShaPasswordEncoder();
}
@Bean
public ReflectionSaltSource saltSource() {
ReflectionSaltSource salt = new ReflectionSaltSource();
salt.setUserPropertyToUse("salt");
return salt;
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider dao = new DaoAuthenticationProvider();
dao.setUserDetailsService(securityService);
dao.setPasswordEncoder(passwordEncoder());
dao.setSaltSource(saltSource());
return dao;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment