네이버 API 로그인2

최우정·2022년 6월 22일
0
post-thumbnail

NaverLogin.html

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<title>네이버 로그인</title>
	<script type="text/javascript" src="https://static.nid.naver.com/js/naverLogin_implicit-1.0.3.js" charset="utf-8"></script>
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
	<!-- 네이버 로그인 버튼 노출 영역 -->
	<div id="naver_id_login"></div>
	<!-- //네이버 로그인 버튼 노출 영역 -->
 	<script type="text/javascript">
	  	var naver_id_login = new naver_id_login("클라이언트 ID", "http://localhost:8088/NaverProject/CallBack.html");
	  	var state = naver_id_login.getUniqState();
	  	naver_id_login.setButton("white", 2,40);
	  	naver_id_login.setDomain("http://localhost:8088/NaverProject/NaverLogin.html");
	  	naver_id_login.setState(state);
	  	naver_id_login.setPopup();
	  	naver_id_login.init_naver_id_login();
 	 </script>
</body>
</html>

CallBack.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
	<script type="text/javascript" src="https://static.nid.naver.com/js/naverLogin_implicit-1.0.3.js" charset="utf-8"></script>
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
	<script type="text/javascript">
	 	var naver_id_login = new naver_id_login("클라이언트 ID", "http://localhost:8088/NaverProject/CallBack.html");
	  	// 접근 토큰 값 출력
	  	$('body').append('<h4>접속토큰:'+naver_id_login.oauthParams.access_token+'</h4>');
	  	// 네이버 사용자 프로필 조회
	  	naver_id_login.get_naver_userprofile("naverSignInCallback()");
	  	// 네이버 사용자 프로필 조회 이후 프로필 정보를 처리할 callback function
	 	function naverSignInCallback() {
	 		alert(naver_id_login.getProfileData('name'));
	 	    alert(naver_id_login.getProfileData('email'));
	 	    
	 		const name = naver_id_login.getProfileData('name');
		    const email = naver_id_login.getProfileData('email');
		    
			let body = $('body');
			body.append('로그인 성공!');
			body.append('<h4>이름:'+name+'</h4>');
			body.append('<h4>이메일:'+email+'</h4>');
			
			setCookie(name, email);
	  	}
			function setCookie(name, email){
		    document.cookie = "name =" + naver_id_login.getProfileData('name');
		    document.cookie = "email =" + naver_id_login.getProfileData('email');
		}
//		window.close();	
		opener.location.href="Insert.jsp";			
	  
</script>
</body>
</html>

Insert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ page import="common2.JDBConnect2"%>    
<%@ page import="java.sql.*" %>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	PreparedStatement pstmt = null;

	// DB 연결
	JDBConnect2 jdbc = new JDBConnect2();
	
	// 쿠키 값 가져오기
	Cookie[] cookie = request.getCookies();
	
	String name = "";
	String email = "";
	
	if(cookie != null){
		for(int i=0; i<cookie.length; i++){
			
			if(cookie[i].getName().equals("name")){
				name = cookie[i].getValue();
			}
			else if(cookie[i].getName().equals("email")){
				email = cookie[i].getValue();
			}

		}
	}
	System.out.print(name+" "+email);
	String sql = "INSERT INTO member2 VALUES(?, ?)";
	pstmt = jdbc.conn.prepareStatement(sql);
	pstmt.setString(1, email);
	pstmt.setString(2, name);
	
	// 쿼리 수행
	int result = pstmt.executeUpdate();
	out.println(result + "행이 입력되었습니다.");
	
	jdbc.close();
%>	
</body>
</html>

JDBConnect.java

package common2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBConnect2 {
	
	public Connection conn;
	public Statement stmt;
	public PreparedStatement pstmt;
	public ResultSet rs;
		
	public JDBConnect2() {
		try {
			// JDBC 드라이버 로드
			Class.forName("oracle.jdbc.OracleDriver");
			System.out.println("드라이버 로드 성공");
				
			//Database 연결
			String url = "jdbc:oracle:thin:@ip주소:1521:XE";
			String id = "c##java";
			String passwd = "java";
			conn = DriverManager.getConnection(url, id, passwd);
			System.out.println("DB 연결 성공");
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
		
	public void close() {
		try {
			if(rs != null) rs.close();
			if(pstmt != null) pstmt.close();
			if(stmt != null) stmt.close();
			if(conn != null) conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}
profile
비전공자 Java, JavaScript, Html, Css, C++ 공부중

0개의 댓글