Java Spring Boot project and Discuz! Forum synchronous login and logout

I transferred from
Java Spring Boot project and Discuz!

My main site Spring Boot has connected with Discuz! -api-for-java has been partially modified to adapt to the current Spring Boot style.

First of all thanks for open source

Opening up with Discuz! is generally divided into API communication and database communication. This time we introduce communication through API, so I have to mention discuz-ucenter-api-for-java. First of all, I would like to thank the original author: Liang Ping (no_ten@163 .com) open-sourced his code, which saved us a lot of time.

Re-modified open source code

After I downloaded the source code, I found that this code is still from 2009, full of retro style, and the original author translated it based on PHP code, so many of the habits in it are not Java habits. The key is to configure the servlet, not Supports Maven builds. This is out of place in the current Spring Boot project, so I modified the source code and released it a second time.
In order to adapt to the Spring Boot style built by Maven, the project address: https://github.com/renfei/discuz-ucenter-api-for-java, I made the following modifications:

  • Publish the project package to the Maven central warehouse
  • Based on the current Java JDK8, the original author's Base64 implementation is removed, and the java.util.Base64 provided by JDK8 is used, so at least JDK8 is required to run.
  • Change the underline style of the method name to camel case, for example, change "uc_user_delete()" to "ucUserDelete()".
  • Change PHP-style variable names to common Java variable names, for example, change "String $module" to "String $module".
  • Change the way of using the configuration file to the way of passing parameters to the construction method when instantiating
  • Change the configuration servlet to define your own Controller and handle HttpServletRequest and HttpServletResponse
  • Some Chinese garbled problems have been fixed

UCenter configuration of Discuz

Before starting the integration, we need to configure the UCenter of Discuz to obtain the interface address, communication key, and APPID. First go to the background UCenter of Discuz, add an application:
insert image description here
then fill in the configuration, select "Other" for the application type, and then give the application a name, fill in your address for the main URL address of the application, and set a password for the communication key by yourself , at the bottom, choose to enable synchronous login and accept notifications:
insert image description here

Spring Boot 集成 discuz-ucenter-api-for-java

I am using the Spring Boot project built by Maven, so I need to modify pom.xml first to introduce discuz-ucenter-api-for-java into the project:

<dependency>
  <groupId>net.renfei</groupId>
  <artifactId>discuz-ucenter-api-for-java</artifactId>
  <version>1.0.7</version>
</dependency>

Then I personally think that it is divided into two parts, one part is to receive messages from Discuz's UCenter as a client; the other part is to actively send messages to Discuz's UCenter, let's talk about it separately.

Receive messages from Discuz's UCenter

First create a Controller, then create a method to process HttpServletRequest and HttpServletResponse, give a UCenter request address @RequestMapping("/api/uc.php"), and instantiate a client net.renfei.discuz.ucenter.api.UCClient and net.renfei.discuz.ucenter.client.Client, then hand over the HttpServletRequest to net.renfei.discuz.ucenter.api.UCClient.doAnswer() for processing, and finally write the result into HttpServletResponse. If the UCenter configuration is correct, it should You can see that the communication is normal in UCenter. The specific use cases are as follows:

@Controller
public class UCenterController {
    
    
    @ResponseBody
    @RequestMapping("/api/uc.php")
    public void uc(HttpServletRequest request, HttpServletResponse response) throws IOException {
    
    
        UCClient ucClient = new UCClient();
        Client client = new Client("http://localhost:8080/uc_server", null, "123456789", "3","");
        String result = ucClient.doAnswer(client, request, response);
        response.getWriter().print(result);
    }
}

Actively send messages to UCenter of Discuz

register

Client client = new Client("http://localhost/uc_server", null, "key", "2","");
String string = client.ucUserRegister("username","password","email");

Log in first and then log in synchronously

Client client = new Client("http://localhost/uc_server", null, "key", "2","");
// 登陆
String result = client.ucUserLogin(uid);
LinkedList<String> rs = XMLHelper.ucUnserialize(result);
if(rs.size() > 0){
    
    
    int uid = Integer.parseInt(rs.get(0));
    String username = rs.get(1);
    String password = rs.get(2);
    String email = rs.get(3);
    if(uid > 0) {
    
    
        //同步登陆
        String string = client.ucUserSynlogin(uid);
        //本地登陆代码
        //TODO ... ....
    } else if(uid == -1) {
    
    
        System.out.println("用户不存在,或者被删除");
    } else if(uid == -2) {
    
    
        System.out.println("密码错");
    } else {
    
    
        System.out.println("未定义");
    }
}else{
    
    
    System.out.println("Login failed");
    System.out.println(result);
}

log in

Client client = new Client("http://localhost/uc_server", null, "key", "2","");
String string = client.ucUserLogin("username","password");

Simultaneous login

Client client = new Client("http://localhost/uc_server", null, "key", "2","");
int UID = 21; //此处是用户的UID(该UID是在论坛用户表per_ucenter_members中的用户UID)
String string = client.ucUserSynlogin(uid);

common problem

The synchronous login interface is successful but there is no login status

  • Symptoms: Call net.renfei.discuz.ucenter.client.Client#ucUserSynlogin successfully, get the JavaScript code, and request the JS address successfully, but there is no login status when accessing Discuz. Check the Response header information of the requested JS address, there is no xxxx_2132_auth cookie in the Set-Cookie.
  • Discovery scene: first call net.renfei.discuz.ucenter.client.Client#ucUserRegister to register user, then call net.renfei.discuz.ucenter.client.Client#ucUserLogin to get uid, and then call net.renfei.discuz. ucenter.client.Client#ucUserSynlogin performs synchronous login to obtain the JavaScript code, and the browser requests the JS address, all of which are in a successful state, but accessing Discuz is not in the login state.
  • Root cause of the problem: After calling net.renfei.discuz.ucenter.client.Client#ucUserRegister for user registration, the user information will be registered in UCenter and inserted into the pre_ucenter_members table, but will not be automatically inserted into the pre_common_member table of the Discuz forum, so call net .renfei.discuz.ucenter.client.Client#ucUserSynlogin Synchronous login is successful. When accessing Discuz, there is no login status because there is no information about this user in Discuz.
  • Solution: The first method: the logic of registered users is directly connected to the database, and the user-related data is inserted into the user-related tables of UCenter and Discuz. The second method: modify the code of UCenter, insert user information into the Discuz user table while inserting into the UCenter user table.
  • Final summary: UCenter is just an intermediate bridge to open up the user accounts of each application, but it will not notify each application of a new user. When the login page of Discuz finds that there is no such user when logging in, it will pull the user information from UCenter Insert your own user table, so after you register a user through the UCenter registration interface, if this user has never logged in to Discuz, then there is no information about this user in Discuz. If you want to log in and verify in a unified way, you need to manually go to Discuz Insert relevant information into the user table of , which involves multiple tables, please refer to the official documentation.

For more information, please read the source code, which will not be demonstrated here. You can raise issues or go to my community forum to discuss: https://bbs.renfei.net/forum-44-1.html

Guess you like

Origin blog.csdn.net/qq_27480007/article/details/128716711