支持跨域调用及登录功能完善
This commit is contained in:
@@ -2,10 +2,12 @@ package com.macro.mall.config;
|
||||
|
||||
import com.macro.mall.bo.AdminUserDetails;
|
||||
import com.macro.mall.component.JwtAuthenticationTokenFilter;
|
||||
import com.macro.mall.component.RestAuthenticationEntryPoint;
|
||||
import com.macro.mall.component.RestfulAccessDeniedHandler;
|
||||
import com.macro.mall.model.UmsAdmin;
|
||||
import com.macro.mall.service.UmsAdminService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -21,6 +23,9 @@ import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
|
||||
/**
|
||||
@@ -32,6 +37,10 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
@Autowired
|
||||
private UmsAdminService adminService;
|
||||
@Autowired
|
||||
private RestfulAccessDeniedHandler restfulAccessDeniedHandler;
|
||||
@Autowired
|
||||
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity httpSecurity) throws Exception {
|
||||
@@ -52,16 +61,22 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
"/v2/api-docs/**"
|
||||
)
|
||||
.permitAll()
|
||||
.antMatchers("/admin/**")// 对于获取token的rest api要允许匿名访问
|
||||
.antMatchers("/admin/login", "/admin/register")// 对登录注册要允许匿名访问
|
||||
.permitAll()
|
||||
.antMatchers("/**")//测试时全部运行访问
|
||||
.antMatchers(HttpMethod.OPTIONS)//跨域请求会先进行一次options请求
|
||||
.permitAll()
|
||||
// .antMatchers("/**")//测试时全部运行访问
|
||||
// .permitAll()
|
||||
.anyRequest()// 除上面外的所有请求全部需要鉴权认证
|
||||
.authenticated();
|
||||
// 禁用缓存
|
||||
httpSecurity.headers().cacheControl();
|
||||
// 添加JWT filter
|
||||
httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
|
||||
//添加自定义未授权和未登录结果返回
|
||||
httpSecurity.exceptionHandling()
|
||||
.accessDeniedHandler(restfulAccessDeniedHandler)
|
||||
.authenticationEntryPoint(restAuthenticationEntryPoint);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,4 +110,21 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
return new JwtAuthenticationTokenFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* 允许跨域调用的过滤器
|
||||
*/
|
||||
@Bean
|
||||
public FilterRegistrationBean corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
config.addAllowedOrigin("*");
|
||||
config.setAllowCredentials(true);
|
||||
config.addAllowedHeader("*");
|
||||
config.addAllowedMethod("*");
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
|
||||
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
|
||||
bean.setOrder(0);
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user