基于java实现单点登陆代码
时间: 2024-01-20 12:04:33 浏览: 104
由于单点登录的实现需要涉及到多个系统之间的交互和认证,因此本文只提供一个简单的单点登录示例代码,仅供参考。
1. 创建一个Java Web项目,命名为sso-server。
2. 在sso-server中创建一个LoginServlet,用于处理用户的登录请求。代码如下:
```
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("admin".equals(username) && "admin".equals(password)) {
String token = UUID.randomUUID().toString();
TokenManager.addToken(token, username);
Cookie cookie = new Cookie("token", token);
cookie.setPath("/");
response.addCookie(cookie);
response.sendRedirect("/sso-client1/index.jsp");
} else {
response.sendRedirect("/sso-server/login.jsp?error=1");
}
}
}
```
3. 创建一个TokenManager,用于管理Token的生成、验证和删除。代码如下:
```
public class TokenManager {
private static Map<String, String> tokenMap = new ConcurrentHashMap<>();
public static String createToken(String username) {
String token = UUID.randomUUID().toString();
tokenMap.put(token, username);
return token;
}
public static boolean checkToken(String token) {
return tokenMap.containsKey(token);
}
public static String getUsername(String token) {
return tokenMap.get(token);
}
public static void removeToken(String token) {
tokenMap.remove(token);
}
}
```
4. 在sso-client1中创建一个IndexServlet,用于验证用户是否已经登录。代码如下:
```
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("token".equals(cookie.getName())) {
String token = cookie.getValue();
if (TokenManager.checkToken(token)) {
String username = TokenManager.getUsername(token);
request.setAttribute("username", username);
request.getRequestDispatcher("/index.jsp").forward(request, response);
return;
}
}
}
}
response.sendRedirect("/sso-server/login.jsp");
}
}
```
5. 在sso-client2中也创建一个IndexServlet,代码和sso-client1中的一样。
6. 在sso-server中创建一个LogoutServlet,用于处理用户的退出请求。代码如下:
```
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("token".equals(cookie.getName())) {
String token = cookie.getValue();
TokenManager.removeToken(token);
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
break;
}
}
}
response.sendRedirect("/sso-server/login.jsp");
}
}
```
7. 最后,在sso-server中创建一个login.jsp页面,让用户输入用户名和密码。代码如下:
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username" />
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
</div>
<div>
<button type="submit">Login</button>
</div>
<c:if test="${param.error ne null}">
<div style="color: red;">Invalid username or password.</div>
</c:if>
</form>
</body>
</html>
```
至此,一个简单的单点登录系统就实现了。当用户在sso-server中登录成功后,sso-server会生成一个Token并将其存储起来,然后将Token以Cookie的形式发送给sso-client1和sso-client2。当用户访问sso-client1或sso-client2时,sso-client会检查Cookie中是否包含Token,如果包含则向sso-server发送Token验证请求,如果验证通过则允许用户访问。当用户在sso-server中点击退出时,sso-server会删除Token,并将Cookie中的Token删除。
阅读全文