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
package com.moral.controller;
 
import com.moral.entity.auth.AuthRole;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.List;
 
@RestController
@RequestMapping("/test")
public class TestController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World";
    }
 
    @RequestMapping(value = "/list_roles", method = RequestMethod.GET)
    @PreAuthorize("hasAnyRole('USER', 'ADMIN')")
    public List<AuthRole> listRoles() {
 
        List<AuthRole> roles = new ArrayList<>();
        AuthRole role1 = new AuthRole();
        role1.setId(1);
        role1.setRole_name("USER");
        roles.add(role1);
 
        AuthRole role2 = new AuthRole();
        role2.setId(2);
        role2.setRole_name("USER");
        roles.add(role2);
 
        return roles;
    }
 
    @RequestMapping(value = "/list_users", method = RequestMethod.GET)
    @PreAuthorize("hasRole('ADMIN')")
    public List<String> listUsers() {
 
        List<String> data = new ArrayList<>();
        data.add("bob");
        data.add("bill");
        data.add("john");
 
        return data;
    }
}