package com.ks.consumerjdgiftcoupon.config;
|
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
@Configuration
|
@EnableWebSecurity
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
class CustomUserService implements UserDetailsService {
|
|
@Override
|
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
|
//通过名称查询用户
|
|
return null;
|
}
|
}
|
|
class CustomAuth implements AuthenticationProvider {
|
|
@Override
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
authentication.setAuthenticated(true);
|
return authentication;
|
}
|
|
@Override
|
public boolean supports(Class<?> aClass) {
|
return true;
|
}
|
}
|
|
|
@Bean
|
UserDetailsService customUserService() {
|
return new CustomUserService();
|
}
|
|
@Bean
|
AuthenticationProvider customAuth() {
|
return new CustomAuth();
|
}
|
|
/**
|
* 定义用户认证规则
|
*
|
* @param auth
|
* @throws Exception
|
*/
|
@Override
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
auth.userDetailsService(customUserService());
|
auth.authenticationProvider(customAuth());
|
}
|
|
/**
|
* 定义授权规则
|
*
|
* @param http
|
* @throws Exception
|
*/
|
@Override
|
protected void configure(HttpSecurity http) throws Exception {
|
http
|
.authorizeRequests()
|
.antMatchers("/", "/home").permitAll()
|
.anyRequest().authenticated()
|
.and()
|
.formLogin()
|
// .loginPage("/login")
|
.permitAll()
|
.and()
|
.logout()
|
.permitAll();
|
}
|
|
|
}
|