Hướng dẫn làm chức năng đăng nhập với spring mvc
Code ví dụ Spring MVC Security, login bằng Facebook Show (Xem thêm: Code ví dụ Spring Boot Security login bằng Facebook) (Xem lại bài Code ví dụ JSP Servlet login bằng Facebook (Java Web) để hiểu nguyên lý đăng nhập bằng tài khoản facebook) Ở bài này mình cũng sẽ không dùng Facebook SDK hay Spring Social mà sẽ tạo thủ công quy trình đăng nhập ứng dụng bằng facebook để mọi người hiểu rõ nguyên lý và luồng chạy. Ở bài sau mình sẽ hướng dẫn sử dụng Spring Social để đăng nhập Facebook) Các công nghệ sử dụng:
Cấu hình https cho tomcatXem lại: cấu hình https cho tomcat Tạo ứng dụng trên facebookỞ đây mình tạo ứng dụng trên facebook để thực hiện chức năng login với app id = ‘359123991240252’ và key = ‘d07e182d8495df6930665d6c39fbe8ac’ (Xem lại: Tạo ứng dụng facebook để đăng nhập thay tài khoản) Tạo Maven ProjectThư viện sử dụng:
Mình sử dụng thêm thư viện httpcomponents và restfb để gửi request bên trong controller tới facebook. Thư viện Jackson để xử lý dữ liệu JSON File cấu hình Spring MVC
package stackjava.com.accessfacebook.controller; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import stackjava.com.accessfacebook.utils.RestFB; @Controller public class BaseController { @Autowired private RestFB restFB; @RequestMapping(value = { "/", "/login" }) public String login(@RequestParam(required = false) String message, final Model model) { }
@RequestMapping("/login-facebook")
public String loginFacebook(HttpServletRequest request) { }
@RequestMapping("/user")
public String user() { }
@RequestMapping("/admin")
public String admin() { }
@RequestMapping("/403")
public String accessDenied() { }
}
Method loginFacebook xử lý kết quả trả về từ facebook
File truy vấn, gửi request tới facebook: package stackjava.com.accessfacebook.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.fluent.Request; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.restfb.DefaultFacebookClient; import com.restfb.FacebookClient; import com.restfb.Version; @Component public class RestFB { public static String FACEBOOK_APP_ID = "359123991240252"; public static String FACEBOOK_APP_SECRET = "d07e182d8495df6930665d6c39fbe8ac"; public static String FACEBOOK_REDIRECT_URL = "https://localhost:8443/AccessFacebook/login-facebook"; public static String FACEBOOK_LINK_GET_TOKEN = "https://graph.facebook.com/oauth/access_token?client_id=%s&client_secret=%s&redirect_uri=%s&code=%s"; public String getToken(final String code) throws ClientProtocolException, IOException { }
public com.restfb.types.User getUserInfo(final String accessToken) { }
public UserDetails buildUser(com.restfb.types.User user) { }
}
Nếu không dùng thư viện RestFB thì bạn có thể dùng URL sau để lấy thông tin của user:
https://graph.facebook.com/me?access_token=... thông tin trả về sẽ gồm id và name.(Bạn cũng có thể lấy thêm nhiều trường khác như email, comment, image… nhưng cần phải có thêm permission, ở đây mình chỉ thực hiện đăng nhập nên chỉ cần permission để lấy public profile) Các file views:<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> Spring MVC-Security Login Form${message}Login FacebookĐường link https://www.facebook.com/dialog/oauth?client_id=180439422588509&redirect_uri=https://localhost:8443/login-facebookdùng để gọi hộp thoại Đăng nhập và cài đặt URL chuyển hướng. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> User PageWelcome: ${pageContext.request.userPrincipal.name}">Admin Page" method="post">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Admin PageWelcome: ${pageContext.request.userPrincipal.name}">User Page" method="post">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
403Hi: ${pageContext.request.userPrincipal.name} you do not have permission to access this page ">User Page" method="post">
Demo:Đăng nhập bình thường bằng tài khoản kai/123456 Đăng nhập bằng tài khoản facebook Trường hợp không đồng ý đăng nhập bằng facebook Trường hợp cho phép đăng nhập bằng facebook (sau khi cho phép, lần sau đăng nhập tiếp facebook sẽ không hỏi lại nữa) |