package com.example.jz.auth; import org.springframework.beans.factory.annotation.Autowired; 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.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; /** * @author 安瑾然 * @data 2022/7/18 - 11:23 AM * @description */ @Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) // 方法增加权限 public class MyTokenSecurityConfig extends WebSecurityConfigurerAdapter { // 将 UserDetailService 注入,使其去查询数据库 @Autowired private UserDetailService userDetailsService; // token 生成器 @Autowired private TokenJwtManager tokenManager; // 自定义密码加密解密 @Autowired private DefaultPwdEncoder defaultPwdEncoder; // 未登录handler @Autowired private MyUnAuthEntryPoint myUnAuthEntryPoint; // 无权限 @Autowired private MyAccessDeniedHandler myAccessDeniedHandler; // 登出handler处理 @Autowired private MyLogoutHandler myLogoutHandler; /** * 登录时,从数据库获取基本信息和权限信息 * * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 设置 userDetailsService 和 密码解析 auth.userDetailsService(userDetailsService).passwordEncoder(defaultPwdEncoder); } /** * 配置访问过滤 * * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { http .exceptionHandling() .authenticationEntryPoint(myUnAuthEntryPoint) // 未登录 handler .accessDeniedHandler(myAccessDeniedHandler) // 无权限 .and().csrf().disable() // 关闭 csrf 跨域请求 .formLogin() .loginProcessingUrl("/login") // 设定登录请求接口 .usernameParameter("username") .passwordParameter("password") .permitAll() .and() .authorizeRequests() // 请求设置 .antMatchers("/user/register", "/webjars/**", "/swagger/**", "/v2/api-docs", "/doc.html", "/swagger-ui.html", "/swagger-resources/**" ).permitAll() // 配置不需要认证的接口 .antMatchers("/wx/login","/wx/isExist","/wx/checkIdentity","/wx/publicity","/minio/upload", "/minio/img/**").permitAll() // 开放小程序的接口 .anyRequest().authenticated() // 任何请求都需要认证 .and() .logout() // logout设定 .logoutUrl("/logout") //退出请求 /logout 未定义,交给自定义handler实现功能 .addLogoutHandler(myLogoutHandler) // 登出 myLogoutHandler 处理 .and() .addFilter(new TokenLoginFilter(tokenManager, authenticationManager())) // 认证交给 自定义 TokenLoginFilter 实现 .addFilter(new TokenAuthFilter(authenticationManager(), tokenManager)) .httpBasic(); } /** * 配置不需要验证的访问路径 * * @param web * @throws Exception */ @Override public void configure(WebSecurity web) throws Exception { // web.ignoring().antMatchers("/test", "/user/login"); web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**"); } }