#
1
3 天以前 69a3c374ca3afb770e3b9ffcbdda07ce362cbf58
1
2
3
4
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.vincent.rsf.openApi.controller.example;
 
import com.vincent.rsf.openApi.entity.dto.CommonResponse;
import com.vincent.rsf.openApi.security.utils.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * App认证使用示例控制器
 * 
 * 演示如何在控制器中使用AppId认证
 * 
 * @author vincent
 * @since 2026-01-05
 */
@RestController
@RequestMapping("/example/auth")
@Api(tags = "App认证使用示例")
public class AppAuthExampleController {
 
    /**
     * 需要App认证的接口示例
     * 
     * @param request HTTP请求
     * @return 响应结果
     */
    @ApiOperation("需要App认证的接口示例")
    @GetMapping("/protected")
    public CommonResponse protectedEndpoint(HttpServletRequest request) {
        // 获取认证的AppId
        String appId = AuthUtils.getAppId(request);
        
        // 检查是否已认证
        if (appId == null) {
            return CommonResponse.error("未通过App认证");
        }
        
        return CommonResponse.ok()
                .setMsg("访问成功")
                .setData("认证的AppId: " + appId);
    }
 
    /**
     * 获取当前认证的App信息
     * 
     * @param request HTTP请求
     * @return App信息
     */
    @ApiOperation("获取当前认证的App信息")
    @GetMapping("/app-info")
    public CommonResponse getAppInfo(HttpServletRequest request) {
        String appId = AuthUtils.getAppId(request);
        
        if (appId == null) {
            return CommonResponse.error("未通过App认证");
        }
        
        return CommonResponse.ok()
                .setMsg("获取App信息成功")
                .setData("当前AppId: " + appId);
    }
 
    /**
     * 无需认证的公开接口
     * 
     * @return 响应结果
     */
    @ApiOperation("无需认证的公开接口")
    @GetMapping("/public")
    public CommonResponse publicEndpoint() {
        return CommonResponse.ok()
                .setMsg("公开接口访问成功")
                .setData("任何人都可以访问此接口");
    }
 
    /**
     * 检查认证状态
     * 
     * @param request HTTP请求
     * @return 认证状态
     */
    @ApiOperation("检查认证状态")
    @PostMapping("/check-auth")
    public CommonResponse checkAuth(HttpServletRequest request) {
        boolean isAuthenticated = AuthUtils.isAuthenticated(request);
        String appId = AuthUtils.getAppId(request);
        
        return CommonResponse.ok()
                .setMsg("认证检查完成")
                .setData("isAuthenticated: " + isAuthenticated + ", appId: " + appId);
    }
}