Skip to content

Commit b9e802e

Browse files
committed
point to point is work
1 parent 37faeaa commit b9e802e

7 files changed

Lines changed: 158 additions & 67 deletions

File tree

springWebSocket/pom.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</parent>
1616

1717
<properties>
18-
<start-class>com.us.WebApplication</start-class>
18+
<start-class>com.us.Application</start-class>
1919
<maven.compiler.target>1.8</maven.compiler.target>
2020
<maven.compiler.source>1.8</maven.compiler.source>
2121
</properties>
@@ -39,6 +39,7 @@
3939
<groupId>org.springframework.boot</groupId>
4040
<artifactId>spring-boot-starter-thymeleaf</artifactId>
4141
</dependency>
42+
4243
<!-- 引入Web模块 -->
4344
<dependency>
4445
<groupId>org.springframework.boot</groupId>
@@ -51,11 +52,12 @@
5152
<artifactId>spring-boot-starter-websocket</artifactId>
5253
</dependency>
5354

55+
<!-- 引入security模块 -->
5456
<dependency>
55-
<groupId>org.springframework</groupId>
56-
<artifactId>spring-messaging</artifactId>
57-
<version>4.2.3.RELEASE</version>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-security</artifactId>
5859
</dependency>
60+
5961
</dependencies>
6062

6163

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
//package com.us.example.config;
2-
//
3-
//import org.springframework.context.annotation.Configuration;
4-
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5-
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6-
//import org.springframework.security.config.annotation.web.builders.WebSecurity;
7-
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8-
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9-
//
10-
//@Configuration
11-
//@EnableWebSecurity
12-
//public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
13-
// @Override
14-
// protected void configure(HttpSecurity http) throws Exception {
15-
// http
16-
// .authorizeRequests()
17-
// .antMatchers("/","/login").permitAll()//1根路径和/login路径不拦截
18-
// .anyRequest().authenticated()
19-
// .and()
20-
// .formLogin()
21-
// .loginPage("/login") //2登陆页面
22-
// .defaultSuccessUrl("/chat") //3登陆成功转向该页面
23-
// .permitAll()
24-
// .and()
25-
// .logout()
26-
// .permitAll();
27-
// }
28-
//
29-
// //4
30-
// @Override
31-
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
32-
// auth
33-
// .inMemoryAuthentication()
34-
// .withUser("wyf").password("wyf").roles("USER")
35-
// .and()
36-
// .withUser("wisely").password("wisely").roles("USER");
37-
// }
38-
// //5忽略静态资源的拦截
39-
// @Override
40-
// public void configure(WebSecurity web) throws Exception {
41-
// web.ignoring().antMatchers("/resources/static/**");
42-
// }
43-
//
44-
//}
1+
package com.us.example.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
10+
@Configuration
11+
@EnableWebSecurity
12+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
13+
@Override
14+
protected void configure(HttpSecurity http) throws Exception {
15+
http
16+
.authorizeRequests()
17+
.antMatchers("/","/login").permitAll()//根路径和/login路径不拦截
18+
.anyRequest().authenticated()
19+
.and()
20+
.formLogin()
21+
.loginPage("/login") //2登陆页面路径为/login
22+
.defaultSuccessUrl("/chat") //3登陆成功转向chat页面
23+
.permitAll()
24+
.and()
25+
.logout()
26+
.permitAll();
27+
}
28+
29+
//4在内存中配置两个用户 wyf 和 wisely ,密码和用户名一致,角色是 USER
30+
@Override
31+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
32+
auth
33+
.inMemoryAuthentication()
34+
.withUser("wyf").password("wyf").roles("USER")
35+
.and()
36+
.withUser("wisely").password("wisely").roles("USER");
37+
}
38+
//5忽略静态资源的拦截
39+
@Override
40+
public void configure(WebSecurity web) throws Exception {
41+
web.ignoring().antMatchers("/resources/static/**");
42+
}
43+
44+
}

springWebSocket/src/main/java/com/us/example/config/WebSocketConfig.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
1818
@Override
1919
public void registerStompEndpoints(StompEndpointRegistry registry) { //endPoint 注册协议节点,并映射指定的URl
2020

21-
registry.addEndpoint("/endpointWisely").withSockJS();//注册一个Stomp 协议的endpoint,并指定 SockJS协议。
22-
// registry.addEndpoint("/endpointChat").withSockJS();
21+
//注册一个Stomp 协议的endpoint,并指定 SockJS协议
22+
registry.addEndpoint("/endpointWisely").withSockJS();
23+
24+
//注册一个名字为"endpointChat" 的endpoint,并指定 SockJS协议。 点对点-用
25+
registry.addEndpoint("/endpointChat").withSockJS();
2326
}
2427

2528

2629
@Override
2730
public void configureMessageBroker(MessageBrokerRegistry registry) {//配置消息代理(message broker)
28-
registry.enableSimpleBroker("/topic"); //广播式应配置一个/topic 消息代理
29-
// registry.enableSimpleBroker("/queue","/topic"); //2
31+
//广播式应配置一个/topic 消息代理
32+
registry.enableSimpleBroker("/topic");
33+
34+
//点对点式增加一个/queue 消息代理
35+
registry.enableSimpleBroker("/queue","/topic");
3036

3137
}
3238
}

springWebSocket/src/main/java/com/us/example/controller/WebSocketController.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
public class WebSocketController {
2424
@Autowired
2525
private WebSocketService ws;
26+
@Autowired
27+
private SimpMessagingTemplate messagingTemplate;
28+
2629

2730
//http://localhost:8080/ws
2831
@MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
@@ -41,19 +44,24 @@ public String say2()throws Exception
4144
return "is ok";
4245
}
4346

44-
// @Autowired
45-
// private SimpMessagingTemplate messagingTemplate;//1
46-
//
47-
// @MessageMapping("/chat")
48-
// public void handleChat(Principal principal, String msg) { //2
49-
// if (principal.getName().equals("wyf")) {//3
50-
// messagingTemplate.convertAndSendToUser("wisely",
51-
// "/queue/notifications", principal.getName() + "-send:"
52-
// + msg);
53-
// } else {
54-
// messagingTemplate.convertAndSendToUser("wyf",
55-
// "/queue/notifications", principal.getName() + "-send:"
56-
// + msg);
57-
// }
58-
// }
47+
@MessageMapping("/chat")
48+
//在springmvc 中可以直接获得principal,principal 中包含当前用户的信息
49+
public void handleChat(Principal principal, Message message) {
50+
51+
/**
52+
* 此处是一段硬编码。如果发送人是wyf 则发送给 wisely 如果发送人是wisely 就发送给 wyf。
53+
*/
54+
if (principal.getName().equals("wyf")) {
55+
//通过convertAndSendToUser 向用户发送信息,
56+
// 第一个参数是接收消息的用户,第二个参数是浏览器订阅的地址,第三个参数是消息本身
57+
58+
messagingTemplate.convertAndSendToUser("wisely",
59+
"/queue/notifications", principal.getName() + "-send:"
60+
+ message.getName());
61+
} else {
62+
messagingTemplate.convertAndSendToUser("wyf",
63+
"/queue/notifications", principal.getName() + "-send:"
64+
+ message.getName());
65+
}
66+
}
5967
}

springWebSocket/src/main/java/com/us/example/service/WebSocketService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
@Service
1313
public class WebSocketService {
14+
1415
@Autowired
16+
//使用SimpMessagingTemplate 向浏览器发送消息
1517
private SimpMessagingTemplate template;
1618

1719
public void sendMessage() throws Exception{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
3+
<html xmlns:th="http://www.thymeleaf.org">
4+
<meta charset="UTF-8" />
5+
<head>
6+
<title>Home</title>
7+
<script th:src="@{sockjs.min.js}"></script>
8+
<script th:src="@{stomp.min.js}"></script>
9+
<script th:src="@{jquery.js}"></script>
10+
</head>
11+
<body>
12+
<p>
13+
聊天室
14+
</p>
15+
16+
<form id="wiselyForm">
17+
<textarea rows="4" cols="60" name="text"></textarea>
18+
<input type="submit"/>
19+
</form>
20+
21+
<script th:inline="javascript">
22+
$('#wiselyForm').submit(function(e){
23+
e.preventDefault();
24+
var text = $('#wiselyForm').find('textarea[name="text"]').val();
25+
sendSpittle(text);
26+
});
27+
//链接endpoint名称为 "/endpointChat" 的endpoint。
28+
var sock = new SockJS("/endpointChat");
29+
var stomp = Stomp.over(sock);
30+
stomp.connect('guest', 'guest', function(frame) {
31+
32+
/**
 订阅了/user/queue/notifications 发送的消息,这里雨在控制器的 convertAndSendToUser 定义的地址保持一致,

33+
* 这里多用了一个/user,并且这个user 是必须的,使用user 才会发送消息到指定的用户。

34+
* */
35+
stomp.subscribe("/user/queue/notifications", handleNotification);
36+
});
37+
38+
39+
40+
function handleNotification(message) {
41+
$('#output').append("<b>Received: " + message.body + "</b><br/>")
42+
}
43+
44+
function sendSpittle(text) {
45+
stomp.send("/chat", {}, JSON.stringify({ 'name': text }));//3
46+
}
47+
$('#stop').click(function() {sock.close()});
48+
</script>
49+
50+
<div id="output"></div>
51+
</body>
52+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3+
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
4+
<meta charset="UTF-8" />
5+
<head>
6+
<title>登陆页面</title>
7+
</head>
8+
<body>
9+
<div th:if="${param.error}">
10+
无效的账号和密码
11+
</div>
12+
<div th:if="${param.logout}">
13+
你已注销
14+
</div>
15+
<form th:action="@{/login}" method="post">
16+
<div><label> 账号 : <input type="text" name="username"/> </label></div>
17+
<div><label> 密码: <input type="password" name="password"/> </label></div>
18+
<div><input type="submit" value="登陆"/></div>
19+
</form>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)