/** * @param client={"key":"xx","scopeIds":[xx,xx]} * @param response_type * @param state * @return */ @POST public HttpResult getCode(Client client, String response_type, String state) { String redirectParam = "?key=" + client.get("key") + "&response_type=" + response_type + "&state=" + state; if (!response_type.equals("code")) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=response_type_not_match")); } Client oldClient = Client.DAO.findFirstBy("key=?", client.get("key")); if (oldClient == null) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=client_not_found")); } else { Set<Scope> scopes = new HashSet<Scope>(); for (Scope scope : oldClient.getScopes()) { if (scope.<Integer>get("selected") == 1 || Arrays.asList(client.getScopeIds()).contains(scope.<Integer>get("id"))) { scopes.add(scope); } } Entity<?> user = Subject.getPrincipal().getModel(); int userId = user.<Integer>get("id"); Code code = new Code(UUID.randomUUID().toString().replaceAll("-", ""), oldClient.<Integer>get("id"), userId, Constant.oauthExpires, scopes); Authorizes.addCode(code);//cache code return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", oldClient.get("redirect_uri") + "?code=" + code.getCode() + "&state=" + state)); } }
String salt = principal.getSalt(); if (salt != null && !salt.isEmpty()) { match = passwordService.match(password, principal.getPassword(), salt); } else { match = passwordService.match(password, principal.getPassword());
public static void logout() { //add cache Principal principal = getPrincipal(); if (principal != null) { logger.debug("Session leave authentication " + principal.getUsername()); } //清理用户 clearPrincipal(); }
/** * 判断是否有当前api权限 * * @param httpMethod httpMethod * @param path path * @return boolean */ public static boolean has(String httpMethod, String path) { String needCredential = need(httpMethod, path); if (needCredential != null) { Principal principal = getPrincipal(); if (principal != null) { if (principal.hasCredential(needCredential)) { return true; } } } else { return true; } return false; }
/** * 权限 * * @param httpMethod httpMethod * @param path path */ public static void check(String httpMethod, String path) { String needCredential = need(httpMethod, path); logger.debug(httpMethod + " " + path + " need credential " + needCredential); if (needCredential != null) { Principal principal = getPrincipal(); if (principal != null) { if (!principal.hasCredential(needCredential)) { throw new HttpException(HttpMessage.FORBIDDEN); } } else { throw new HttpException(HttpMessage.UNAUTHORIZED); } } }
String salt = principal.getSalt(); if (salt != null && !salt.isEmpty()) { match = passwordService.match(password, principal.getPassword(), salt); } else { match = passwordService.match(password, principal.getPassword());
/** * 判断是否有当前api权限 * * @param httpMethod httpMethod * @param path path * @return boolean */ public static boolean has(String httpMethod, String path) { String needCredential = need(httpMethod, path); if (needCredential != null) { Principal principal = getPrincipal(); if (principal != null) { if (principal.hasCredential(needCredential)) { return true; } } } else { return true; } return false; }
public static void logout() { //add cache Principal principal = getPrincipal(); if (principal != null) { logger.debug("Session leave authentication " + principal.getUsername()); } //清理用户 clearPrincipal(); }
/** * 权限 * * @param httpMethod httpMethod * @param path path */ public static void check(String httpMethod, String path) { String needCredential = need(httpMethod, path); logger.debug(httpMethod + " " + path + " need credential " + needCredential); if (needCredential != null) { Principal principal = getPrincipal(); if (principal != null) { if (!principal.hasCredential(needCredential)) { throw new HttpException(HttpMessage.FORBIDDEN); } } else { throw new HttpException(HttpMessage.UNAUTHORIZED); } } }