@Test public void testLoginWithAuthedUser() throws AccessException { when(authManager.login(request)).thenReturn(user); when(authConfigs.getNacosAuthSystemType()).thenReturn(AuthSystemTypes.NACOS.name()); when(authConfigs.getTokenValidityInSeconds()).thenReturn(18000L); Object actual = userController.login("nacos", "nacos", response, request); assertThat(actual, instanceOf(JsonNode.class)); String actualString = actual.toString(); assertTrue(actualString.contains("\"accessToken\":\"1234567890\"")); assertTrue(actualString.contains("\"tokenTtl\":18000")); assertTrue(actualString.contains("\"globalAdmin\":true")); }
@PostMapping("/login") public Object login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, HttpServletRequest request) throws AccessException { if (AuthSystemTypes.NACOS.name().equalsIgnoreCase(authConfigs.getNacosAuthSystemType())) { NacosUser user = (NacosUser) authManager.login(request); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, NacosAuthConfig.TOKEN_PREFIX + user.getToken()); ObjectNode result = JacksonUtils.createEmptyJsonNode(); result.put(Constants.ACCESS_TOKEN, user.getToken()); result.put(Constants.TOKEN_TTL, authConfigs.getTokenValidityInSeconds()); result.put(Constants.GLOBAL_ADMIN, user.isGlobalAdmin()); return result; } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); RestResult<String> rr = new RestResult<String>(); try { Authentication authentication = authenticationManager.authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication); String token = jwtTokenUtils.createToken(authentication); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, "Bearer " + token); rr.setCode(200); rr.setData("Bearer " + token); return rr; } catch (BadCredentialsException authentication) { rr.setCode(401); rr.setMessage("Login failed"); return rr; } }
UserController { @PostMapping("/login") public Object login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, HttpServletRequest request) throws AccessException { if (AuthSystemTypes.NACOS.name().equalsIgnoreCase(authConfigs.getNacosAuthSystemType())) { NacosUser user = (NacosUser) authManager.login(request); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, NacosAuthConfig.TOKEN_PREFIX + user.getToken()); ObjectNode result = JacksonUtils.createEmptyJsonNode(); result.put(Constants.ACCESS_TOKEN, user.getToken()); result.put(Constants.TOKEN_TTL, authConfigs.getTokenValidityInSeconds()); result.put(Constants.GLOBAL_ADMIN, user.isGlobalAdmin()); return result; } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); RestResult<String> rr = new RestResult<String>(); try { Authentication authentication = authenticationManager.authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication); String token = jwtTokenUtils.createToken(authentication); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, "Bearer " + token); rr.setCode(200); rr.setData("Bearer " + token); return rr; } catch (BadCredentialsException authentication) { rr.setCode(401); rr.setMessage("Login failed"); return rr; } } }
UserController { @PostMapping("/login") public Object login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, HttpServletRequest request) throws AccessException { if (AuthSystemTypes.NACOS.name().equalsIgnoreCase(authConfigs.getNacosAuthSystemType())) { NacosUser user = (NacosUser) authManager.login(request); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, NacosAuthConfig.TOKEN_PREFIX + user.getToken()); ObjectNode result = JacksonUtils.createEmptyJsonNode(); result.put(Constants.ACCESS_TOKEN, user.getToken()); result.put(Constants.TOKEN_TTL, authConfigs.getTokenValidityInSeconds()); result.put(Constants.GLOBAL_ADMIN, user.isGlobalAdmin()); return result; } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); RestResult<String> rr = new RestResult<String>(); try { Authentication authentication = authenticationManager.authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication); String token = jwtTokenUtils.createToken(authentication); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, "Bearer " + token); rr.setCode(200); rr.setData("Bearer " + token); return rr; } catch (BadCredentialsException authentication) { rr.setCode(401); rr.setMessage("Login failed"); return rr; } }  }
UserController { @PostMapping("/login") public Object login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, HttpServletRequest request) throws AccessException { if (AuthSystemTypes.NACOS.name().equalsIgnoreCase(authConfigs.getNacosAuthSystemType())) { NacosUser user = (NacosUser) authManager.login(request); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, NacosAuthConfig.TOKEN_PREFIX + user.getToken()); ObjectNode result = JacksonUtils.createEmptyJsonNode(); result.put(Constants.ACCESS_TOKEN, user.getToken()); result.put(Constants.TOKEN_TTL, authConfigs.getTokenValidityInSeconds()); result.put(Constants.GLOBAL_ADMIN, user.isGlobalAdmin()); return result; } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); RestResult<String> rr = new RestResult<String>(); try { Authentication authentication = authenticationManager.authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication); String token = jwtTokenUtils.createToken(authentication); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, "Bearer " + token); rr.setCode(200); rr.setData("Bearer " + token); return rr; } catch (BadCredentialsException authentication) { rr.setCode(401); rr.setMessage("Login failed"); return rr; } }  @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) @PostMapping Object createUser(@RequestParam String username, @RequestParam String password); @DeleteMapping @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) Object deleteUser(@RequestParam String username); @PutMapping @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) Object updateUser(@RequestParam String username, @RequestParam String newPassword); @GetMapping @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.READ) Object getUsers(@RequestParam int pageNo, @RequestParam int pageSize); @PostMapping("/login") Object login(@RequestParam String username, @RequestParam String password, HttpServletResponse response,
            HttpServletRequest request); @PutMapping("/password") @Deprecated RestResult<String> updatePassword(@RequestParam(value = "oldPassword") String oldPassword,
            @RequestParam(value = "newPassword") String newPassword); @GetMapping("/search") @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) List<String> searchUsersLikeUsername(@RequestParam String username); }
UserController { @PostMapping("/login") public Object login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, HttpServletRequest request) throws AccessException { if (AuthSystemTypes.NACOS.name().equalsIgnoreCase(authConfigs.getNacosAuthSystemType())) { NacosUser user = (NacosUser) authManager.login(request); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, NacosAuthConfig.TOKEN_PREFIX + user.getToken()); ObjectNode result = JacksonUtils.createEmptyJsonNode(); result.put(Constants.ACCESS_TOKEN, user.getToken()); result.put(Constants.TOKEN_TTL, authConfigs.getTokenValidityInSeconds()); result.put(Constants.GLOBAL_ADMIN, user.isGlobalAdmin()); return result; } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); RestResult<String> rr = new RestResult<String>(); try { Authentication authentication = authenticationManager.authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication); String token = jwtTokenUtils.createToken(authentication); response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, "Bearer " + token); rr.setCode(200); rr.setData("Bearer " + token); return rr; } catch (BadCredentialsException authentication) { rr.setCode(401); rr.setMessage("Login failed"); return rr; } }  @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) @PostMapping Object createUser(@RequestParam String username, @RequestParam String password); @DeleteMapping @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) Object deleteUser(@RequestParam String username); @PutMapping @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) Object updateUser(@RequestParam String username, @RequestParam String newPassword); @GetMapping @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.READ) Object getUsers(@RequestParam int pageNo, @RequestParam int pageSize); @PostMapping("/login") Object login(@RequestParam String username, @RequestParam String password, HttpServletResponse response,
            HttpServletRequest request); @PutMapping("/password") @Deprecated RestResult<String> updatePassword(@RequestParam(value = "oldPassword") String oldPassword,
            @RequestParam(value = "newPassword") String newPassword); @GetMapping("/search") @Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE) List<String> searchUsersLikeUsername(@RequestParam String username);  }