forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
console/src/main/java/com/alibaba/nacos/console/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.console.controller; | ||
|
||
import com.alibaba.nacos.WebSecurityConfig; | ||
import com.alibaba.nacos.console.utils.JWTTokenUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">hxy1991</a> | ||
*/ | ||
@RestController("auth") | ||
@RequestMapping("/v1/auth") | ||
public class AuthController { | ||
|
||
@Autowired | ||
private JWTTokenUtils jwtTokenUtils; | ||
@Autowired | ||
private AuthenticationManager authenticationManager; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AuthController.class); | ||
|
||
/** | ||
* Whether the Nacos is in broken states or not, and cannot recover except by being restarted | ||
* | ||
* @return HTTP code equal to 200 indicates that Nacos is in right states. HTTP code equal to 500 indicates that | ||
* Nacos is in broken states. | ||
*/ | ||
|
||
@ResponseBody | ||
@RequestMapping(value = "login", method = RequestMethod.GET) | ||
public String login(HttpServletRequest request, HttpServletResponse response, | ||
@RequestParam("username") String username, | ||
@RequestParam("password") String password) throws Exception { | ||
// 通过用户名和密码创建一个 Authentication 认证对象,实现类为 UsernamePasswordAuthenticationToken | ||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); | ||
// TODO: 去数据库查询是否存在该用户 | ||
try { | ||
//通过 AuthenticationManager(默认实现为ProviderManager)的authenticate方法验证 Authentication 对象 | ||
Authentication authentication = authenticationManager.authenticate(authenticationToken); | ||
//将 Authentication 绑定到 SecurityContext | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
//生成Token | ||
String token = jwtTokenUtils.createToken(authentication); | ||
//将Token写入到Http头部 | ||
response.addHeader(WebSecurityConfig.AUTHORIZATION_HEADER, "Bearer " + token); | ||
return "Bearer " + token; | ||
} catch (BadCredentialsException authentication) { | ||
throw new Exception("密码错误"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters