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 listRoles() { List 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 listUsers() { List data = new ArrayList<>(); data.add("bob"); data.add("bill"); data.add("john"); return data; } }