144 lines
5.8 KiB
Java
144 lines
5.8 KiB
Java
package net.alawn.bill.security;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
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.config.BeanIds;
|
|
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.builders.WebSecurity;
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
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.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
import org.springframework.security.web.firewall.HttpFirewall;
|
|
import org.springframework.security.web.firewall.StrictHttpFirewall;
|
|
import org.springframework.util.DigestUtils;
|
|
|
|
/**
|
|
* @Author: Alawn
|
|
* @Description:
|
|
* @Date: Create in 16:53 2019-05-17
|
|
*/
|
|
@Configuration
|
|
@EnableWebSecurity
|
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
@Qualifier("authUserDetailsService")
|
|
@Autowired
|
|
private UserDetailsService userDetailsService;
|
|
|
|
@Autowired
|
|
private JwtAuthenticationEntryPoint unauthorizedHandler;
|
|
|
|
@Bean
|
|
public JwtAuthenticationTokenFilter authenticationTokenFilterBean(){
|
|
return new JwtAuthenticationTokenFilter();
|
|
}
|
|
|
|
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
|
|
@Override
|
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
|
return super.authenticationManagerBean();
|
|
}
|
|
|
|
@Autowired
|
|
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
|
|
authenticationManagerBuilder
|
|
// 设置UserDetailsService
|
|
.userDetailsService(this.userDetailsService)
|
|
// 使用BCrypt进行密码的hash
|
|
.passwordEncoder(passwordEncoder());
|
|
|
|
}
|
|
|
|
/** 装载BCrypt密码编码器 */
|
|
@Bean
|
|
public PasswordEncoder passwordEncoder() {
|
|
return new BCryptPasswordEncoder();
|
|
}
|
|
|
|
/** 自定义防火墙 */
|
|
@Bean
|
|
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
|
|
StrictHttpFirewall firewall = new StrictHttpFirewall();
|
|
firewall.setAllowUrlEncodedSlash(true);
|
|
firewall.setAllowSemicolon(true);
|
|
return firewall;
|
|
}
|
|
|
|
/** Security Http 配置 */
|
|
@Override
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
http.csrf().disable() // 去掉跨域
|
|
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
|
|
.and()
|
|
.authorizeRequests()
|
|
.antMatchers("/").permitAll()
|
|
.antMatchers("/druid/**").permitAll()
|
|
.antMatchers("/swagger*").permitAll()
|
|
.antMatchers( HttpMethod.GET, "/","/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js").permitAll()
|
|
.antMatchers("/auth/**").permitAll()
|
|
.antMatchers("/bill/**").authenticated()
|
|
.antMatchers("/basic/**/*").authenticated()
|
|
// .antMatchers("/auth/**").permitAll().anyRequest().authenticated()
|
|
.and()
|
|
.formLogin().loginPage("/").permitAll()
|
|
.and()
|
|
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 去掉Session
|
|
.and()
|
|
.logout().permitAll() ;
|
|
|
|
// 添加JWT logger
|
|
http.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
|
|
|
|
// 禁用缓存
|
|
http.headers().cacheControl(); // 禁用缓存
|
|
}
|
|
|
|
/** Security Web 配置 */
|
|
@Override
|
|
public void configure(WebSecurity web) throws Exception {
|
|
super.configure(web);
|
|
// @formatter:off
|
|
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
|
|
}
|
|
|
|
/** Security Authentication 配置 */
|
|
@Autowired
|
|
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
|
// auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
|
|
|
|
//校验用户
|
|
auth.userDetailsService(userDetailsService).passwordEncoder( new PasswordEncoder() {
|
|
//对密码进行加密
|
|
@Override
|
|
public String encode(CharSequence charSequence) {
|
|
System.out.println(charSequence.toString());
|
|
return DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
|
|
}
|
|
//对密码进行判断匹配
|
|
@Override
|
|
public boolean matches(CharSequence charSequence, String s) {
|
|
String encode = DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
|
|
boolean res = s.equals( encode );
|
|
return res;
|
|
}
|
|
} );
|
|
}
|
|
// @Override
|
|
// public void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
// // 使用自定义身份验证组件
|
|
// auth.authenticationProvider(new CustomAuthenticationProvider(userDetailsService));
|
|
// }
|
|
|
|
}
|