Fangniuke Forum Project Summary

1. What fields do each table in the database have?

user table user table

(1) id user's id (2) username username (3) password password (4) salt salt (5) email

(6) type type (7) status status (8) activationCode activation code (9) headerUrl avatar storage path
(10) createTime creation time

discuss_post post table

(1) id The id of each post

(2) user_id records which user posted this post and records the user's id. Obviously, this field can be used to associate with the user table.

(3) title the title of the post

(4) content The content of the post. Since the content of the post is relatively long, the data type of this field is text.

(5) type type of post, 0 means this is a normal post, 1 means this is a pinned post

(6) status The status of the post, 0 means this is a normal post, 1 means it is a highlight post, 2 means it has been blocked.

(7) create_time The creation time of the post. The data type is timestamp.

(8) comment_count indicates the number of comments on this post

(9) score is the score of the post, used to rank the post.

commentcomment form

(1) id primary key

(2) user_id who commented

(3) entity_type who is commenting (for example, 1 represents commenting on the post and 2 represents commenting on the post comment)

(4) entity_id is the ID of the person who commented. For example, if you comment on post A, it is the primary key ID of post A.

(5) content The specific content of the comment

(6) status 0 means normal, status 1 means unavailable

(7) create_time creation time

login_ticket login credential table

(1) id primary key field

(2) user_id indicates which user's login credentials this row is

(3) Ticket credential: a random string as a unique identifier

(4) Status, whether the voucher is valid, 0 means normal, 1 means invalid

(5) expired When does it expire? The data type is timestamp, which means that the voucher will automatically expire when it expires.

message table private message table

(1) id primary key field

(2) from_id the id of the message sender

(3) to_id id of the message receiver

(4) conversation_id is used to uniquely identify this conversation (for example, from_id is 111, to_id is 112, so conversation_id is 111_112, from_id is 112, to_id is 111, conversation_id is still 111_112)

(5) content content sent

(6) status 0 means unread, 1 means read

(7) create_time represents the sending time

2. Function

1. Paging query function: each page displays ten posts, go to the discussion_post table to search (if the user is logged in, the blocked posts will not display status! = 2)

select id,user_id,title,content,type,status,create_time,comment_count,score
from discuss_post
where status!=2
order by type desc,create_time desc
limit 0  10

type=1 means pinned posts, first put the pinned posts in front, then compare the creation order, and sort according to the creation order.

Limit is followed by two parameters. The first parameter is the line number of the starting line of each page. The second parameter is the maximum number of pieces of data to be displayed on each page.

Another function: The post function I have published is also searched in the discussion_post table, and the row in this table where the user_id field is equal to the logged-in user's id.

2. User registration function

 Click the registration button on the homepage to register:

 Submit data through the form, and the backend checks whether the registered account already exists and whether the email address has been registered (check in the user table)

select xxxxx  from  user  where  email=xxxxx

Then send an activation email to the entered email address (the email sending function is implemented here by importing the maven coordinate dependency of spring mail)

The user clicks the link in the activation email, registers successfully, and jumps to the login page.

Successfully inserted a piece of data into the user table

4.Exit function

Modify the login credentials to an invalid state, and then jump to the homepage of the website (not logged in)

5. Use an interceptor to prevent users from logging in and making some pages inaccessible.

6.Publish a post

Click publish, enter the title and content, click publish, and then submit it to the database through ajax. The page is still on the previous page.

In fact, it is to add a row of data to the discussion_post table.

You need to log in before you can post

Specify sensitive words and use a prefix tree to replace all sensitive words with *

7. Comment on the post

Add a row of data to the comment table

8. Like function

9. Use kafka to send notifications to users

When someone comments on a user's post, send a notification to the user

When someone likes a user's post, send a notification to the user

When someone follows a user, send a notification to the user

 Comments, likes, and followers are each a topic.

This is a forum system that implements functions such as login, registration, posting, commenting, likes, and following.

Technology stack: SpringMVC, Mybatis, Springboot, Mybatis,

And use AOP to record logs uniformly

Use kafka to asynchronously send system notifications (likes, follows, comments) to users

Where redis is used in the project:

(1) Store user login credentials

(2) Implement likes using the set data structure. The key is the id of the like object, and the value stores the id of the liker.

(3) To implement attention, use the zset data structure (ordered set). The key is the ID of the person being followed, the value is the ID of the person being followed, and the score is the time of attention.

(4) Store the verification code required when logging in

(5) Use the HyperLogLog data structure in Redis to count UVs


 

Interview FAQs:

1. Let’s talk about IOC and AOP

2.SpringMVC

3. How to implement the registration function

Click the registration button on the homepage to register:

 Submit data through the form. After receiving the data, the server checks in the background whether the account has been registered and the mailbox has been registered. If not, the server sends an activation email to the registered mailbox (send mail function, by importing spring mail maven coordinate dependency), the user clicks the link in the mailbox and successfully activates

In fact, it is to insert a row of data into the user table

What needs attention here is: md5 encryption. When the database stores the user's password, it stores the MD5 value (128 bits) of the user's password. In this way, even if criminals get the MD5 value of the user password in the database, they will not be able to know the user's password. When the user logs in, if the md5 encryption result of the password entered by the user is equal to the MD5 value in the database, it means that the password is correct.

More specifically, the password and salt are string concatenated (string = password + salt)

Then encrypt the concatenated string with md5

//生成盐,其实就是长度为5的字符串
user.setSalt(CommunityUtil.generateUUID().substring(0,5));

//密码和盐进行拼接
String  password=user.getPassword()+user.getSalt();

//进行md5加密后存入数据库
user.setPassword(CommunityUtil.md5(password);

At the beginning, the status field is 0, indicating invalid. After activation processing, it becomes 1, indicating valid.

4. Login function

Use kaptcha tool class to generate verification code

After the login is successful, the login credentials are generated (insert a piece of data into the login_ticket login credentials table), and then the credentials are issued to the client (just return the ticket field, which is a string, to the client, and put the ticket in the cookie), page Jump to home page index.html

If failed, jump back to login page 

5. Where is the login status saved?

It is initially saved in the database. There is a login_ticket login table, which has a ticket field. When the user logs in, a row of data will be inserted into this table.

When a request comes, check whether there is a ticket string in the request. If the ticket string is equal to the ticket field in a record in the login_ticket table, if yes, it means that the user is logged in. If the user logs out, put the ticket string in the request. ticket deletion

Later, redis was used for optimization. The key is the ticket string and the value is the serialized object.

After using Redis to store login credentials,login_ticket表就不需要了

6. Where is AOP used in the project?

When printing logs, AOP is used when printing logs before method calls.

AOP aspect-oriented programming_Pr Young's blog-CSDN blog

Encapsulate the print log function into an aspect and weave it into the connection point position of the target object.

7. How to use redis in the project

Redis stores the verification code: it was previously stored in the session on the server side, but now it is stored in redis (the expiration time is set to 60s).

The user refreshes the verification code and generates a random string fae737adc5eb41eaa189334f9d152d4e. The value of the verification code is 4ZBT and is stored in the cookie:

kaptchaOwner:fae737adc5eb41eaa189334f9d152d4e //Delete after 60s

 Stored in redis:

fae737adc5eb41eaa189334f9d152d4e:4ZBT

8.Ajax: partial refresh

9. How to implement the like function?

  This is a very frequently used function, especially if a big-name account posts a post, a large number of people will like it in a short period of time.

//执行点赞  

//第一个参数表示点赞用户的id,第二个参数是帖子的id
//表示userid这个用户给entityid这个帖子点赞
  
public void  like(int userId,int entityId)
{
   
     

}

Using the set data structure, the key is the ID of the liked object (that is, the ID of the post), and the value stores the ID of the person who liked it, so a kv key-value pair represents which users have liked a post.

For example: Users with IDs 275, 376, and 555 like the post with ID 123. The following key-value pairs are stored in redis:

123->{275,376,555}

Like it for the first time and cancel it for the second time (when you like it for the second time, first check whether there is already a key-value of post id-user id. If so, it means it was liked repeatedly for the second time. So cancel the like, that is, remove the user's id from the set)

At the same time, you can also display the total number of likes on a post (query the number of likes on a certain post: pass in the id of the post and find the number of likes)

10. How to implement the follow and unfollow functions:

It also uses redis. The key is the ID of the person being followed, and the value is a set composed of the ID of the person being followed. It is very similar to the like function. Score is the time of following.

11. Post search using Elasticsearch

After the post is published, it is saved to the Elasticsearch server.

elasticsearchService.searchDiscussPost()

12.SpringSecurity replaces interceptors for login checks

I would still say use an interceptor to check if the user is logged in.

Guess you like

Origin blog.csdn.net/weixin_47414034/article/details/128504308