Files
quinn-bbc/src/main/java/com/quinn/config/SecurityConfig.java
2022-05-15 21:38:11 +08:00

74 lines
3.1 KiB
Java

package com.quinn.config;
import com.quinn.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserServiceImpl userService;
//请求授权验证
@Override
protected void configure(HttpSecurity http) throws Exception {
// .denyAll(); //拒绝访问
// .authenticated(); //需认证通过
// .permitAll(); //无条件允许访问
// 访问权限
http.authorizeRequests()
.antMatchers("/","/index","/favicon.ico").permitAll()
.antMatchers("/register","/login","/toLogin","/checkLogin","/checkRegister").permitAll()
.antMatchers("/source","/source/view/*").permitAll()
.antMatchers("/blog","/blog/read/*").permitAll()
.antMatchers("/search/**").permitAll()
.antMatchers("//about").permitAll()
.antMatchers("/hotspot").permitAll()
.antMatchers("/blog/**").authenticated()
.antMatchers("/source/**").authenticated()
.antMatchers("/user/**").authenticated()
.antMatchers("/wx/**").authenticated()
.antMatchers("/*").authenticated();
// 登录配置
http.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/toLogin")
.loginProcessingUrl("/login") // 登陆表单提交请求
.defaultSuccessUrl("/index"); // 设置默认登录成功后跳转的页面
// 注销配置
http.headers().contentTypeOptions().disable();
http.headers().frameOptions().disable(); // 图片跨域
http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
http.logout().logoutSuccessUrl("/");
// 记住我配置 默认为remember-me
// http.rememberMe().rememberMeParameter("remember");
}
// 用户授权验证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
}
// 密码加密方式
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}