跨域手动配置
This commit is contained in:
parent
d209c360db
commit
fea726f2ed
|
@ -0,0 +1,32 @@
|
||||||
|
package com.example.filter;
|
||||||
|
|
||||||
|
import com.example.utils.Const;
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpFilter;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Order(Const.ORDER_CORS)
|
||||||
|
public class CorsFilter extends HttpFilter {
|
||||||
|
@Override
|
||||||
|
protected void doFilter(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
FilterChain chain) throws IOException, ServletException {
|
||||||
|
this.addCorsHeader(request , response);
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addCorsHeader(HttpServletRequest request,
|
||||||
|
HttpServletResponse response){
|
||||||
|
response.addHeader("Access-Control-Allow-Origin" , request.getHeader("Origin"));
|
||||||
|
response.addHeader("Access-Control-Allow-Methods", "GET , POST , PUT, DELETE , OPTIONS");
|
||||||
|
response.addHeader("Access-Control-Allow-Headers", "Authorization , Content-Type ");
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,4 +2,7 @@ package com.example.utils;
|
||||||
|
|
||||||
public class Const {
|
public class Const {
|
||||||
public static final String JWT_BLACK_LIST = "jwt:blacklist:";
|
public static final String JWT_BLACK_LIST = "jwt:blacklist:";
|
||||||
|
|
||||||
|
public static final int ORDER_CORS = -102;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
security:
|
security:
|
||||||
|
filter:
|
||||||
|
order: -100 #默认值为-100
|
||||||
jwt:
|
jwt:
|
||||||
key: abcdefgh
|
key: abcdefgh
|
||||||
expire: 7
|
expire: 7
|
||||||
|
|
Loading…
Reference in New Issue