ruanwenjun commented on a change in pull request #5422: URL: https://github.com/apache/dolphinscheduler/pull/5422#discussion_r627263351
########## File path: dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/AuthUtil.java ########## @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.api.utils; + +import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.dao.entity.User; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.util.ObjectUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +public class AuthUtil { + + private AuthUtil() { + throw new IllegalStateException("AuthUtil class"); + } + + /** + * get LoginUser info from request + * @return User + */ + public static User user() { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); Review comment: I think a better implementation is to extract user from `LoginHandlerInterceptor` and store in `ThreadLocal`. `RequestContextHolder` use the same way to store `RequestAttributes`, but adds additional operations to get `SESSION_USER`. BTW, another advantage of not storing the user in request is that most agents will log the request information, this will expose the user to the log. ########## File path: dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/AuthUtil.java ########## @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.api.utils; + +import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.dao.entity.User; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.util.ObjectUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +public class AuthUtil { Review comment: Rename to `AuthUtils`, consistent with other naming ########## File path: dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java ########## @@ -74,20 +72,18 @@ @PostMapping(value = "/create") @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_ACCESS_TOKEN_ERROR) - @AccessLogAnnotation(ignoreRequestArgs = "loginUser") - public Result createToken(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "userId") int userId, + @AccessLogAnnotation() + public Result createToken(@RequestParam(value = "userId") int userId, @RequestParam(value = "expireTime") String expireTime, @RequestParam(value = "token") String token) { - Map<String, Object> result = accessTokenService.createToken(loginUser, userId, expireTime, token); + Map<String, Object> result = accessTokenService.createToken(AuthUtil.user(), userId, expireTime, token); Review comment: I think the user parameters in the `Service` can also be removed ########## File path: dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/AuthUtil.java ########## @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.api.utils; + +import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.dao.entity.User; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.util.ObjectUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +public class AuthUtil { + + private AuthUtil() { + throw new IllegalStateException("AuthUtil class"); + } + + /** + * get LoginUser info from request + * @return User + */ + public static User user() { Review comment: It is better to rename to `getCurrentUser` or `getCurrentLoginUser`, methods should be verbs. https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
