跨域 屮·

This commit is contained in:
wangliwen 2022-06-02 13:53:40 +08:00
parent 5a41ac20c7
commit 641e6ea2d8
2 changed files with 25 additions and 16 deletions

View File

@ -8,7 +8,6 @@ import com.yawei.pso.SSOResponse;
import com.yawei.pso.TicketManager; import com.yawei.pso.TicketManager;
import io.renren.common.constant.Constant; import io.renren.common.constant.Constant;
import io.renren.common.interceptor.YaweiSSOProperties; import io.renren.common.interceptor.YaweiSSOProperties;
import io.renren.common.utils.HttpContextUtils;
import io.renren.common.utils.Result; import io.renren.common.utils.Result;
import io.renren.modules.security.service.SysUserTokenService; import io.renren.modules.security.service.SysUserTokenService;
import io.renren.modules.security.user.SecurityUser; import io.renren.modules.security.user.SecurityUser;
@ -35,11 +34,10 @@ import java.util.Map;
/** /**
* oauth2过滤器 * oauth2过滤器
*
*/ */
@Component() @Component()
@Scope("prototype") @Scope("prototype")
public class Oauth2Filter extends AuthenticatingFilter { public class Oauth2Filter extends AuthenticatingFilter {
@Autowired @Autowired
private YaweiSSOProperties yaweiSSOProperties; private YaweiSSOProperties yaweiSSOProperties;
@ -55,7 +53,7 @@ public class Oauth2Filter extends AuthenticatingFilter {
//获取请求token //获取请求token
String token = getRequestToken((HttpServletRequest) request); String token = getRequestToken((HttpServletRequest) request);
if(StringUtils.isBlank(token)){ if (StringUtils.isBlank(token)) {
return null; return null;
} }
@ -152,7 +150,7 @@ public class Oauth2Filter extends AuthenticatingFilter {
private void send401Error(HttpServletResponse response, String msg) throws IOException { private void send401Error(HttpServletResponse response, String msg) throws IOException {
response.setContentType("application/json;charset=utf-8"); response.setContentType("application/json;charset=utf-8");
response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin()); response.setHeader("Access-Control-Allow-Origin", "*");
response.setStatus(HttpStatus.SC_UNAUTHORIZED); response.setStatus(HttpStatus.SC_UNAUTHORIZED);
Result r = new Result().error(HttpStatus.SC_UNAUTHORIZED, msg); Result r = new Result().error(HttpStatus.SC_UNAUTHORIZED, msg);
@ -167,7 +165,7 @@ public class Oauth2Filter extends AuthenticatingFilter {
/** /**
* 获取请求的token * 获取请求的token
*/ */
private String getRequestToken(HttpServletRequest httpRequest){ private String getRequestToken(HttpServletRequest httpRequest) {
//从header中获取token //从header中获取token
String token = httpRequest.getHeader(Constant.TOKEN_HEADER); String token = httpRequest.getHeader(Constant.TOKEN_HEADER);
@ -176,7 +174,7 @@ public class Oauth2Filter extends AuthenticatingFilter {
} }
//如果header中不存在token则从参数中获取token //如果header中不存在token则从参数中获取token
if(StringUtils.isBlank(token)){ if (StringUtils.isBlank(token)) {
token = httpRequest.getParameter(Constant.TOKEN_HEADER); token = httpRequest.getParameter(Constant.TOKEN_HEADER);
} }
@ -200,7 +198,7 @@ public class Oauth2Filter extends AuthenticatingFilter {
private boolean yaweiHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { private boolean yaweiHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 获取当前请求的url // 获取当前请求的url
String requestUri = request.getHeader("REQUESTURI"); String requestUri = request.getHeader("REQUESTURI");
if (requestUri == null){ if (requestUri == null) {
requestUri = request.getRequestURI(); requestUri = request.getRequestURI();
} }

View File

@ -4,7 +4,8 @@
* https://www.renren.io * https://www.renren.io
* <p> * <p>
* 版权所有侵权必究 * 版权所有侵权必究
*/package io.renren.websocket; */
package io.renren.websocket;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import io.renren.common.constant.Constant; import io.renren.common.constant.Constant;
@ -12,6 +13,8 @@ import io.renren.websocket.config.WebSocketConfig;
import io.renren.websocket.data.MessageData; import io.renren.websocket.data.MessageData;
import io.renren.websocket.data.WebSocketData; import io.renren.websocket.data.WebSocketData;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.websocket.*; import javax.websocket.*;
@ -23,12 +26,17 @@ import java.util.concurrent.ConcurrentHashMap;
/** /**
* WebSocket服务 * WebSocket服务
*
*/ */
@Slf4j @Slf4j
@Component @Component
@ServerEndpoint(value = "/websocket", configurator = WebSocketConfig.class) @ServerEndpoint(value = "/websocket", configurator = WebSocketConfig.class)
public class WebSocketServer { public class WebSocketServer {
private static Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
public WebSocketServer() {
logger.info("-------------websocket----------------");
}
/** /**
* 客户端连接信息 * 客户端连接信息
*/ */
@ -56,13 +64,14 @@ public class WebSocketServer {
@OnMessage @OnMessage
public void onMessage(Session session, String msg) { public void onMessage(Session session, String msg) {
log.info("session id: " + session.getId()+" message" + msg); log.info("session id: " + session.getId() + " message" + msg);
} }
/** /**
* 发送信息 * 发送信息
* @param userIdList 用户ID列表 *
* @param message 消息内容 * @param userIdList 用户ID列表
* @param message 消息内容
*/ */
public void sendMessage(List<Long> userIdList, MessageData<?> message) { public void sendMessage(List<Long> userIdList, MessageData<?> message) {
userIdList.forEach(userId -> sendMessage(userId, message)); userIdList.forEach(userId -> sendMessage(userId, message));
@ -70,8 +79,9 @@ public class WebSocketServer {
/** /**
* 发送信息 * 发送信息
* @param userId 用户ID *
* @param message 消息内容 * @param userId 用户ID
* @param message 消息内容
*/ */
public void sendMessage(Long userId, MessageData<?> message) { public void sendMessage(Long userId, MessageData<?> message) {
servers.values().forEach(info -> { servers.values().forEach(info -> {
@ -83,7 +93,8 @@ public class WebSocketServer {
/** /**
* 发送信息给全部用户 * 发送信息给全部用户
* @param message 消息内容 *
* @param message 消息内容
*/ */
public void sendMessageAll(MessageData<?> message) { public void sendMessageAll(MessageData<?> message) {
servers.values().forEach(info -> sendMessage(info.getSession(), message)); servers.values().forEach(info -> sendMessage(info.getSession(), message));