Просмотр исходного кода

Auth相关代码逻辑分层,用于后续真正的做服务化分离。

huangbo лет назад: 4
Родитель
Сommit
4e235addae

+ 11 - 10
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/control/AuthController.java

@ -2,24 +2,28 @@ package com.ai.ipu.server.control;
2 2
3 3
import org.slf4j.Logger;
4 4
import org.slf4j.LoggerFactory;
5
import org.springframework.beans.factory.annotation.Autowired;
5 6
import org.springframework.stereotype.Controller;
6 7
import org.springframework.web.bind.annotation.RequestMapping;
7 8
import org.springframework.web.bind.annotation.ResponseBody;
8 9
9
import com.ai.ipu.basic.file.FileUtil;
10 10
import com.ai.ipu.data.JMap;
11 11
import com.ai.ipu.data.impl.JsonMap;
12 12
import com.ai.ipu.server.handler.AuthHandler;
13 13
import com.ai.ipu.server.handler.HandlerManager;
14
import com.ai.ipu.server.service.AuthService;
14 15
15 16
@Controller
16 17
public class AuthController {
17 18
    private static final Logger log = LoggerFactory.getLogger(AuthController.class);
19
    
20
    /**自动实例化AuthServiceImpl*/
21
    @Autowired
22
    private AuthService authService;
23
    
18 24
    /**
19 25
     * 登录
20 26
     * @param param
21
     * @return
22
     * @throws Exception
23 27
     */
24 28
    @ResponseBody
25 29
    @RequestMapping("/login")
@ -30,11 +34,11 @@ public class AuthController {
30 34
        String password= param.getString("password");
31 35
        
32 36
        /*校验账号密码*/
33
        AuthHandler handler = HandlerManager.takeAuthHandler();
34 37
        JMap result = new JsonMap();
35
        if(handler.checkValid(username, password)){
38
        if(authService.login(username, password)){
36 39
            result.put("msg", "校验成功");
37 40
        }else{
41
            AuthHandler handler = HandlerManager.takeAuthHandler();
38 42
            result.put("msg", handler.authFailMessage(username));
39 43
        }
40 44
        return result;
@ -42,17 +46,14 @@ public class AuthController {
42 46
    /**
43 47
     * 获取菜单
44 48
     * @param param
45
     * @return
46
     * @throws Exception
47 49
     */
48 50
    @ResponseBody
49 51
    @RequestMapping("/menu")
50 52
    public JMap takeMenu(JMap param) throws Exception {
51 53
        log.debug("获取菜单");
52 54
        /*获取账号*/
53
        //String username = param.getString("username");
54
        String menuJson = FileUtil.readFile(AuthController.class.getClassLoader().getResourceAsStream("menu.json"));
55
        /*校验账号密码*/
55
        String username = param.getString("username");
56
        String menuJson = authService.menu(username);
56 57
        JMap result = new JsonMap();
57 58
        result.put("menu",menuJson);
58 59
        return result;

+ 9 - 0
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/service/AuthService.java

@ -0,0 +1,9 @@
1
package com.ai.ipu.server.service;
2

3
public interface AuthService {
4
    
5
    public boolean login(String username, String password) throws Exception;
6
    
7
    public String menu(String username) throws Exception;
8

9
}

+ 33 - 0
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/service/impl/AuthServiceImpl.java

@ -0,0 +1,33 @@
1
package com.ai.ipu.server.service.impl;
2

3
import org.springframework.stereotype.Service;
4

5
import com.ai.ipu.basic.file.FileUtil;
6
import com.ai.ipu.server.control.AuthController;
7
import com.ai.ipu.server.handler.AuthHandler;
8
import com.ai.ipu.server.handler.HandlerManager;
9
import com.ai.ipu.server.service.AuthService;
10

11
/**
12
 * @author huangbo@asiainfo.com
13
 * @team IPU
14
 * @date 2020年10月12日下午6:28:26
15
 * @desc 两个实现类时报错:expected single matching bean but found 2
16
 */
17
@Service
18
public class AuthServiceImpl implements AuthService{
19

20
    @Override
21
    public boolean login(String username, String password) throws Exception {
22
        /*校验账号密码*/
23
        AuthHandler handler = HandlerManager.takeAuthHandler();
24
        return handler.checkValid(username, password);
25
    }
26

27
    @Override
28
    public String menu(String username) throws Exception {
29
        /*读取默认的菜单配置*/
30
        String menuJson = FileUtil.readFile(AuthController.class.getClassLoader().getResourceAsStream("menu.json"));
31
        return menuJson;
32
    }
33
}