package cn.com.flashspace.wms.databus.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.core.GrantedAuthorityDefaults;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
GrantedAuthorityDefaults grantedAuthorityDefaults() {
// 去除 ROLE_ 前缀
return new GrantedAuthorityDefaults("");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 关闭csrf防护
.csrf().disable()
.headers().frameOptions().disable()
.and();
http
//登录处理
.formLogin() // //表单方式,或httpBasic
// .loginPage("/login").permitAll() // 自定义登录页面login.html, 并放行
// .loginProcessingUrl("/form")
// .defaultSuccessUrl("/index") //成功登陆后跳转页面
// .failureUrl("/loginError").permitAll()
.and().logout().permitAll()
.and();
http
// 授权配置
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/*.html", "/**/*.html", "/**/*.css", "/**/*.js").permitAll()
.antMatchers("/swagger-ui.html").permitAll().antMatchers("/swagger-resources/**").permitAll()
.antMatchers("/wms-databus/receiveMsg/**", "/wms-databus/sendMsg/**").hasRole("USER") // 指定访问用户权限
// .anyRequest().authenticated() // 默认其它的请求都需要认证
.anyRequest().permitAll() // 默认其它的请求都不需要认证
.and();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("admin").password(passwordEncoder().encode("admin1234")).roles("ADMIN")
.and()
.withUser("flash").password(passwordEncoder().encode("flash1234")).roles("USER");
}
}
Spring Security
最新推荐文章于 2024-07-21 23:44:17 发布