├── README.md
├── pom.xml
└── src
└── main
├── java
└── com
│ └── example
│ └── demo
│ ├── MyDemoApplication.java
│ ├── config
│ └── WebSocketConfig.java
│ ├── controller
│ ├── ChatController.java
│ ├── PageController.java
│ └── UserController.java
│ ├── dao
│ ├── RelationMapper.java
│ └── UserMapper.java
│ ├── message
│ ├── BaseMessage.java
│ └── ChatMessage.java
│ ├── model
│ ├── Relation.java
│ └── User.java
│ ├── security
│ ├── AnyUserDetailsService.java
│ ├── UserPrincipal.java
│ └── WebSecurityConfig.java
│ ├── service
│ ├── RelationService.java
│ └── UserService.java
│ ├── utils
│ └── Constants.java
│ └── vo
│ └── BaseUser.java
└── resources
├── application.properties
├── generatorConfig.xml
├── mapper
├── RelationMapper.xml
└── UserMapper.xml
├── mybatis-config.xml
└── templates
├── chat.html
├── login.html
└── register.html
/README.md:
--------------------------------------------------------------------------------
1 | # simple-chat-room
2 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.example
7 | myDemo
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | myDemo
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 1.5.9.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 | 3.0.0.RELEASE
26 | 2.0.0
27 |
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-web
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-starter-websocket
37 |
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter-test
42 | test
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-security
47 |
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-starter-thymeleaf
52 |
53 |
54 | org.thymeleaf.extras
55 | thymeleaf-extras-springsecurity4
56 |
57 |
58 |
59 | mysql
60 | mysql-connector-java
61 |
62 |
63 | org.mybatis.spring.boot
64 | mybatis-spring-boot-starter
65 | 1.1.1
66 |
67 |
68 | tk.mybatis
69 | mapper-spring-boot-starter
70 | 1.1.4
71 |
72 |
73 | org.projectlombok
74 | lombok
75 | 1.16.16
76 |
77 |
78 | com.alibaba
79 | fastjson
80 | 1.2.30
81 |
82 |
83 |
84 | org.webjars
85 | webjars-locator
86 |
87 |
88 | org.webjars
89 | sockjs-client
90 | 1.0.2
91 |
92 |
93 | org.webjars
94 | stomp-websocket
95 | 2.3.3
96 |
97 |
98 | org.webjars
99 | bootstrap
100 | 3.3.7
101 |
102 |
103 | org.webjars
104 | jquery
105 | 3.1.0
106 |
107 |
108 |
109 |
110 |
111 |
112 | org.springframework.boot
113 | spring-boot-maven-plugin
114 |
115 |
116 | org.mybatis.generator
117 | mybatis-generator-maven-plugin
118 | 1.3.2
119 |
120 | true
121 | true
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/MyDemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | @MapperScan("com.example.demo.dao")
9 | public class MyDemoApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(MyDemoApplication.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/config/WebSocketConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.config;
2 |
3 | import org.springframework.context.annotation.Configuration;
4 | import org.springframework.messaging.simp.config.MessageBrokerRegistry;
5 | import org.springframework.web.socket.CloseStatus;
6 | import org.springframework.web.socket.WebSocketHandler;
7 | import org.springframework.web.socket.WebSocketSession;
8 | import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
9 | import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
10 | import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
11 | import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
12 | import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
13 | import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;
14 |
15 | @Configuration
16 | @EnableWebSocketMessageBroker
17 | public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
18 |
19 | @Override
20 | public void registerStompEndpoints(StompEndpointRegistry registry) {
21 | //添加服务端点,接收客户端的连接
22 | registry.addEndpoint("/any-socket")
23 | //开启SockJS支持
24 | .withSockJS();
25 | }
26 |
27 | @Override
28 | public void configureMessageBroker(MessageBrokerRegistry registry) {
29 | //服务端接收地址的前缀
30 | registry.setApplicationDestinationPrefixes("/app");
31 | //客户端订阅地址的前缀信息
32 | registry.enableSimpleBroker("/topic","/user");
33 | }
34 |
35 | @Override
36 | public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
37 | registration.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() {
38 |
39 | @Override
40 | public WebSocketHandler decorate(WebSocketHandler handler) {
41 | return new WebSocketHandlerDecorator(handler){
42 | /* (non-Javadoc)
43 | * 客户端与服务端建立连接后的服务端的操作
44 | */
45 | @Override
46 | public void afterConnectionEstablished(WebSocketSession session) throws Exception {
47 | String username = session.getPrincipal().getName();
48 | System.out.println("online:"+username);
49 | super.afterConnectionEstablished(session);
50 | }
51 |
52 | /* (non-Javadoc)
53 | * 客户端与服务端断开连接后服务端的操作
54 | */
55 | @Override
56 | public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)
57 | throws Exception {
58 | String username = session.getPrincipal().getName();
59 | System.out.println("offline:"+username);
60 | super.afterConnectionClosed(session, closeStatus);
61 | }
62 | };
63 | }
64 | });
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/controller/ChatController.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.controller;
2 |
3 | import java.security.Principal;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Date;
6 |
7 |
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.messaging.handler.annotation.MessageMapping;
10 | import org.springframework.messaging.handler.annotation.SendTo;
11 | import org.springframework.messaging.simp.SimpMessagingTemplate;
12 | import org.springframework.stereotype.Controller;
13 |
14 | import com.alibaba.fastjson.JSON;
15 | import com.example.demo.message.BaseMessage;
16 | import com.example.demo.message.ChatMessage;
17 | import com.example.demo.utils.Constants;
18 |
19 | @Controller
20 | public class ChatController {
21 |
22 | private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
23 |
24 | @Autowired
25 | private SimpMessagingTemplate template;
26 |
27 | /**
28 | * 群聊
29 | * @param principal 当前用户
30 | * @param message 接收到的客户端的消息
31 | * @return 包装后的消息
32 | */
33 | @MessageMapping("/all")//接收发送到 /{服务端接收地址的前缀}/all 地址的消息
34 | @SendTo("/topic/all")//将return的结果发送到 /topic/all 地址
35 | public String all(Principal principal,String message){
36 | BaseMessage baseMessage = new BaseMessage();
37 | baseMessage.setType(Constants.TO_ALL);
38 | baseMessage.setContent(message);
39 | baseMessage.setSender(principal.getName());
40 | baseMessage.setSendTime(format.format(new Date()));
41 | return JSON.toJSONString(baseMessage);
42 | }
43 |
44 | /**
45 | * 群发图片
46 | * @param principal
47 | * @param message
48 | * @return
49 | */
50 | @MessageMapping("/pic/all")//接收发送到 /{服务端接收地址的前缀}/all 地址的消息
51 | @SendTo("/topic/pic/all")//将return的结果发送到 /topic/all 地址
52 | public String picAll(Principal principal,String message){
53 | return message;
54 | }
55 |
56 | @MessageMapping("/pic/chat")//接收发送到 /{服务端接收地址的前缀}/chat 地址的消息
57 | public void pic2One(Principal principal,String message){
58 | ChatMessage chatMessage = JSON.parseObject(message,ChatMessage.class);
59 | BaseMessage baseMessage = new BaseMessage();
60 | baseMessage.setType(Constants.TO_ONE);
61 | baseMessage.setSender(principal.getName());
62 | System.out.println(principal.getName());
63 | baseMessage.setContent(chatMessage.getContent());
64 | baseMessage.setSendTime(format.format(new Date()));
65 |
66 | //转发包装后的消息至/{user}/topic/chat地址
67 | template.convertAndSendToUser(chatMessage.getReceiver(), "/topic/pic/chat", JSON.toJSONString(baseMessage));
68 | }
69 |
70 | /**
71 | * 群发文件
72 | * @param principal
73 | * @param message
74 | * @return
75 | */
76 | @MessageMapping("/file/all")//接收发送到 /{服务端接收地址的前缀}/all 地址的消息
77 | @SendTo("/topic/file/all")//将return的结果发送到 /topic/all 地址
78 | public String fileAll(Principal principal,String message){
79 | return message;
80 | }
81 |
82 | @MessageMapping("/file/chat")//接收发送到 /{服务端接收地址的前缀}/chat 地址的消息
83 | public void file2One(Principal principal,String message){
84 | ChatMessage chatMessage = JSON.parseObject(message,ChatMessage.class);
85 | BaseMessage baseMessage = new BaseMessage();
86 | baseMessage.setType(Constants.TO_ONE);
87 | baseMessage.setSender(principal.getName());
88 | System.out.println(principal.getName());
89 | baseMessage.setContent(chatMessage.getContent());
90 | baseMessage.setSendTime(format.format(new Date()));
91 |
92 | //转发包装后的消息至/{user}/topic/chat地址
93 | template.convertAndSendToUser(chatMessage.getReceiver(), "/topic/file/chat", JSON.toJSONString(baseMessage));
94 | }
95 |
96 | /**
97 | * 私聊
98 | * @param principal
99 | * @param message
100 | */
101 | @MessageMapping("/chat")//接收发送到 /{服务端接收地址的前缀}/chat 地址的消息
102 | public void chat(Principal principal,String message){
103 | ChatMessage chatMessage = JSON.parseObject(message,ChatMessage.class);
104 | BaseMessage baseMessage = new BaseMessage();
105 | baseMessage.setType(Constants.TO_ONE);
106 | baseMessage.setSender(principal.getName());
107 | System.out.println(principal.getName());
108 | baseMessage.setContent(chatMessage.getContent());
109 | baseMessage.setSendTime(format.format(new Date()));
110 |
111 | //转发包装后的消息至/{user}/topic/chat地址
112 | template.convertAndSendToUser(chatMessage.getReceiver(), "/topic/chat", JSON.toJSONString(baseMessage));
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/controller/PageController.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.controller;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.security.core.annotation.AuthenticationPrincipal;
7 | import org.springframework.stereotype.Controller;
8 | import org.springframework.ui.Model;
9 | import org.springframework.web.bind.annotation.GetMapping;
10 |
11 | import com.example.demo.security.UserPrincipal;
12 | import com.example.demo.service.RelationService;
13 | import com.example.demo.vo.BaseUser;
14 |
15 | @Controller
16 | public class PageController {
17 |
18 | @Autowired
19 | private RelationService relationService;
20 |
21 | /**
22 | * @return 跳转url地址别名或者物理跳转地址
23 | */
24 | @GetMapping(value="/login")
25 | public String login(){
26 | return "login";
27 | }
28 | @GetMapping(value="/register")
29 | public String register(){
30 | return "register";
31 | }
32 |
33 | @GetMapping(value="/Pic")
34 | public String Pic(){
35 | return "Pic";
36 | }
37 |
38 | /**
39 | * @param userPrincipal @AuthenticationPrincipal可获取当前登录用户
40 | * @param model 用于向页面传递数据
41 | * @return
42 | */
43 | @GetMapping(value="/chat")
44 | public String chat(@AuthenticationPrincipal UserPrincipal userPrincipal, Model model){
45 | model.addAttribute("user", userPrincipal);
46 | List friends = relationService.getFriendsByUsername(userPrincipal.getUsername());
47 | model.addAttribute("friends", friends);
48 | return "chat";
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/controller/UserController.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.controller;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | import javax.servlet.http.HttpServletRequest;
7 |
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.security.core.annotation.AuthenticationPrincipal;
10 | import org.springframework.stereotype.Controller;
11 | import org.springframework.web.bind.annotation.PostMapping;
12 | import org.springframework.web.bind.annotation.RequestParam;
13 | import org.springframework.web.bind.annotation.ResponseBody;
14 |
15 | import com.example.demo.model.User;
16 | import com.example.demo.security.UserPrincipal;
17 | import com.example.demo.service.RelationService;
18 | import com.example.demo.service.UserService;
19 |
20 | @Controller
21 | public class UserController {
22 | @Autowired
23 | UserService userService;
24 | @Autowired
25 | RelationService relationService;
26 |
27 | @PostMapping(value="/register")
28 | @ResponseBody
29 | public boolean register(HttpServletRequest request){
30 | String username = request.getParameter("username");
31 | String password = request.getParameter("password");
32 | String nickname = request.getParameter("nickname");
33 | User user = new User(username,password,nickname);
34 | if(null != userService.selectByUsername(user.getUsername())){
35 | return false;
36 | }
37 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
38 | user.setRegisterData(dateFormat.format(new Date()));
39 | return userService.save(user);
40 | }
41 |
42 | @PostMapping(value="/addfriend")
43 | @ResponseBody
44 | public String addfriend(@AuthenticationPrincipal UserPrincipal userPrincipal,@RequestParam String friend){
45 | return relationService.addFriend(userPrincipal.getUsername(),friend);
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/dao/RelationMapper.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.dao;
2 |
3 | import java.util.List;
4 |
5 | import org.apache.ibatis.annotations.Param;
6 |
7 | import com.example.demo.model.User;
8 |
9 | import tk.mybatis.mapper.common.Mapper;
10 | import tk.mybatis.mapper.common.MySqlMapper;
11 |
12 | public interface RelationMapper extends Mapper, MySqlMapper{
13 |
14 | List getFriendsByUsername(String username);
15 |
16 | void addFriend(@Param("username")String username,@Param("friend")String friend);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/dao/UserMapper.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.dao;
2 |
3 | import com.example.demo.model.User;
4 |
5 | import tk.mybatis.mapper.common.Mapper;
6 | import tk.mybatis.mapper.common.MySqlMapper;
7 |
8 | public interface UserMapper extends Mapper, MySqlMapper{
9 |
10 | int insert(User record);
11 |
12 | User selectByUsername(String username);
13 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/message/BaseMessage.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.message;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class BaseMessage {
7 | public String getSender() {
8 | return sender;
9 | }
10 | public void setSender(String sender) {
11 | this.sender = sender;
12 | }
13 | public String getSendTime() {
14 | return sendTime;
15 | }
16 | public void setSendTime(String sendTime) {
17 | this.sendTime = sendTime;
18 | }
19 | public String getContent() {
20 | return content;
21 | }
22 | public void setContent(String content) {
23 | this.content = content;
24 | }
25 | private String sender;
26 | private String sendTime;
27 | private String content;
28 | private String type;
29 | public String getType() {
30 | return type;
31 | }
32 | public void setType(String type) {
33 | this.type = type;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/message/ChatMessage.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.message;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class ChatMessage{
7 | private String receiver;
8 | private String content;
9 | private String type;
10 |
11 | public String getContent() {
12 | return content;
13 | }
14 |
15 | public void setContent(String content) {
16 | this.content = content;
17 | }
18 |
19 | public String getType() {
20 | return type;
21 | }
22 |
23 | public void setType(String type) {
24 | this.type = type;
25 | }
26 |
27 | public String getReceiver() {
28 | return receiver;
29 | }
30 |
31 | public void setReceiver(String receiver) {
32 | this.receiver = receiver;
33 | }
34 |
35 | // public static void main(String[] args) {
36 | // ChatMessage chatMessage = new ChatMessage();
37 | // chatMessage.setContent("chat");
38 | // chatMessage.setSender("qwe");
39 | // chatMessage.setReceiver("zxc");
40 | // chatMessage.setSendTime("2018-1-31");
41 | // System.out.println(JSON.toJSON(chatMessage));
42 | // }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/model/Relation.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.model;
2 |
3 | import javax.persistence.Column;
4 | import javax.persistence.Table;
5 |
6 | @Table(name="liusong.relation")
7 | public class Relation {
8 | @Column(name="id")
9 | private Integer id;
10 | @Column(name="username")
11 | private String username;
12 | @Column(name="friend")
13 | private String friend;
14 | public Integer getId() {
15 | return id;
16 | }
17 | public void setId(Integer id) {
18 | this.id = id;
19 | }
20 | public String getUsername() {
21 | return username;
22 | }
23 | public void setUsername(String username) {
24 | this.username = username;
25 | }
26 | public String getFriend() {
27 | return friend;
28 | }
29 | public void setFriend(String friend) {
30 | this.friend = friend;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/model/User.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.model;
2 |
3 | import javax.persistence.Column;
4 | import javax.persistence.Table;
5 |
6 | @Table(name = "liusong.user")
7 | public class User {
8 | @Column(name="id")
9 | private Integer id;
10 | @Column(name="nick_name")
11 | private String nickName;
12 | @Column(name="password")
13 | private String password;
14 | @Column(name="register_data")
15 | private String registerData;
16 | @Column(name="username")
17 | private String username;
18 |
19 | public User() {
20 | super();
21 | }
22 |
23 | public User(String nickName, String password, String username) {
24 | super();
25 | this.nickName = nickName;
26 | this.password = password;
27 | this.username = username;
28 | }
29 |
30 | public Integer getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Integer id) {
35 | this.id = id;
36 | }
37 |
38 | public String getNickName() {
39 | return nickName;
40 | }
41 |
42 | public void setNickName(String nickName) {
43 | this.nickName = nickName == null ? null : nickName.trim();
44 | }
45 |
46 | public String getPassword() {
47 | return password;
48 | }
49 |
50 | public void setPassword(String password) {
51 | this.password = password == null ? null : password.trim();
52 | }
53 |
54 | public String getRegisterData() {
55 | return registerData;
56 | }
57 |
58 | public void setRegisterData(String registerData) {
59 | this.registerData = registerData == null ? null : registerData.trim();
60 | }
61 |
62 | public String getUsername() {
63 | return username;
64 | }
65 |
66 | public void setUsername(String username) {
67 | this.username = username == null ? null : username.trim();
68 | }
69 |
70 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/security/AnyUserDetailsService.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.security;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.security.core.GrantedAuthority;
8 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
9 | import org.springframework.security.core.userdetails.UserDetails;
10 | import org.springframework.security.core.userdetails.UserDetailsService;
11 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
12 | import org.springframework.stereotype.Service;
13 |
14 | import com.example.demo.model.User;
15 | import com.example.demo.service.UserService;
16 |
17 | @Service
18 | public class AnyUserDetailsService implements UserDetailsService{
19 |
20 | private final static String ROLE_TAG = "ROLE_USER";
21 |
22 | @Autowired
23 | private UserService userService;
24 |
25 | @Override
26 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
27 | User user = userService.selectByUsername(username);
28 | if(user==null){
29 | throw new UsernameNotFoundException("用户不存在!");
30 | }
31 | List authorities = new ArrayList<>();
32 | authorities.add(new SimpleGrantedAuthority(ROLE_TAG));
33 | UserPrincipal userPrincipal = new UserPrincipal(username,user.getPassword(),authorities);
34 | userPrincipal.setNickName(user.getNickName());
35 | return userPrincipal;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/security/UserPrincipal.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.security;
2 |
3 | import java.util.Collection;
4 |
5 | import org.springframework.security.core.GrantedAuthority;
6 | import org.springframework.security.core.userdetails.User;
7 |
8 | public class UserPrincipal extends User{
9 |
10 | private static final long serialVersionUID = -3962444933610725267L;
11 |
12 | private String nickName;
13 |
14 | public String getNickName() {
15 | return nickName;
16 | }
17 |
18 | public void setNickName(String nickName) {
19 | this.nickName = nickName;
20 | }
21 |
22 | public UserPrincipal(String username, String password, Collection extends GrantedAuthority> authorities) {
23 | super(username, password, authorities);
24 | }
25 |
26 | public UserPrincipal(String username, String password, boolean enabled, boolean accountNonExpired,
27 | boolean credentialsNonExpired, boolean accountNonLocked,
28 | Collection extends GrantedAuthority> authorities) {
29 | super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/security/WebSecurityConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.security;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
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 |
14 | @Autowired
15 | private AnyUserDetailsService anyUserDetailsService;
16 |
17 | @Autowired
18 | public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
19 | auth.userDetailsService(this.anyUserDetailsService);
20 | }
21 |
22 | @Override
23 | protected void configure(HttpSecurity http) throws Exception {
24 | http.authorizeRequests().antMatchers("/","/register","/api/common/**").permitAll()
25 | .anyRequest().authenticated().and().formLogin().loginPage("/login").defaultSuccessUrl("/chat", true)
26 | .permitAll().and().logout().permitAll().and().csrf().disable();;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/service/RelationService.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | import com.example.demo.dao.RelationMapper;
10 | import com.example.demo.model.User;
11 | import com.example.demo.vo.BaseUser;
12 |
13 | @Service
14 | public class RelationService {
15 |
16 | @Autowired
17 | private UserService userService;
18 |
19 | @Autowired
20 | private RelationMapper relationMapper;
21 |
22 | public List getFriendsByUsername(String username){
23 | List list = relationMapper.getFriendsByUsername(username);
24 | List friends = new ArrayList<>();
25 | for(String l:list){
26 | BaseUser bu = new BaseUser(l);
27 | User user = userService.selectByUsername(l);
28 | bu.setNickName(user.getNickName());
29 | friends.add(bu);
30 | }
31 | return friends;
32 | }
33 |
34 | public String addFriend(String username, String friend){
35 | if(null == userService.selectByUsername(friend)){
36 | System.out.println("用户不存在");
37 | return "用户不存在!";
38 | }
39 | if(username.equals(friend)){
40 | System.out.println("不能添加本人!");
41 | return "不能添加本人!";
42 | }
43 | if(relationMapper.getFriendsByUsername(username)!=null&&relationMapper.getFriendsByUsername(username).contains(friend)){
44 | System.out.println("已是好友");
45 | return "已是好友!";
46 | }
47 | relationMapper.addFriend(username,friend);
48 | relationMapper.addFriend(friend, username);
49 | return "添加成功!";
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.service;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Service;
5 |
6 | import com.example.demo.dao.UserMapper;
7 | import com.example.demo.model.User;
8 |
9 | @Service
10 | public class UserService {
11 | @Autowired
12 | UserMapper userMapper;
13 |
14 | public boolean save(User user){
15 | userMapper.insert(user);
16 | return true;
17 | }
18 |
19 | public User selectByUsername(String username){
20 | return userMapper.selectByUsername(username);
21 | }
22 |
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/utils/Constants.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.utils;
2 |
3 | public class Constants {
4 | public final static String TO_ALL = "to_all";
5 | public final static String TO_ONE = "to_one";
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/vo/BaseUser.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.vo;
2 |
3 | public class BaseUser {
4 | private String name;
5 | private String nickName;
6 |
7 | public String getNickName() {
8 | return nickName;
9 | }
10 |
11 | public void setNickName(String nickName) {
12 | this.nickName = nickName;
13 | }
14 |
15 | public BaseUser(String name) {
16 | this.name = name;
17 | }
18 |
19 | public String getName() {
20 | return name;
21 | }
22 |
23 | public void setName(String name) {
24 | this.name = name;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2 | spring.datasource.url=jdbc:mysql://localhost:3306/liusong?useUnicode=true&characterEncoding=utf-8
3 | spring.datasource.username=root
4 | spring.datasource.password=123456
5 | logging.level.org.springframework.security=info
6 | spring.thymeleaf.cache=false
7 |
8 | mybatis.type-aliases-package=com.example.demo.model
9 | mybatis.mapper-locations=classpath:mapper/*.xml
10 | mybatis.config=classpath:mybatis-config.xml
--------------------------------------------------------------------------------
/src/main/resources/generatorConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/main/resources/mapper/RelationMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | id, username, friend
12 |
13 |
14 |
17 |
18 |
19 | INSERT INTO relation (username,friend) VALUES (#{username},#{friend})
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/main/resources/mapper/UserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 | insert into user (nick_name, password,
13 | register_data, username)
14 | values (#{nickName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
15 | #{registerData,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR})
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/main/resources/templates/chat.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | chat
6 |
7 |
8 |
9 |
10 |
11 |
12 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
34 |
35 |
36 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
[当前] - [所有人]
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
247 |
248 |
--------------------------------------------------------------------------------
/src/main/resources/templates/login.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Login
6 |
7 |
8 |
16 |
17 |
--------------------------------------------------------------------------------
/src/main/resources/templates/register.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | register
6 |
7 |
8 |
9 |
24 |
25 |
44 |
45 |
--------------------------------------------------------------------------------