게시판에서 클라이언트 ip를 가져오고 싶은데 어떻게 해야 좋을까요?

게시판에서 클라이언트 ip를 가져오고 싶은데 어떻게 해야 좋을까요?

QA

게시판에서 클라이언트 ip를 가져오고 싶은데 어떻게 해야 좋을까요?

답변 1

본문


<form method="post" action="fboardInsert" autocomplete="off"
enctype="multipart/form-data">
<input type="hidden" name="fb_reip" value="${fb_reip}">
<div class="mb-3">
<label for="boardTitle">제목</label>
<input type="text" class="form-control" id="subject" name="fb_subject"
placeholder="제목을 입력해 주세요">
</div>
<br>
<div class="mb-3">
<label for="reg_id">작성자</label>
<input type="text" name="fb_writer" class="form-control" id="fb_writer"
th:value="${session.sessionNick}" readonly="readonly">
</div>
<br>
<div class="mb-3">
<label for="boardContent">내용</label>
<textarea class="form-control" rows="5" id="content"
name="fb_content" placeholder="내용을 입력해 주세요"></textarea>
</div>
<div class="mb-3">
<label for="mfile">사진업로드</label>
<input type="file" name="mfile" class="form-control" id="mfile">
</div>
<div class="col-sm-10">
<img th:src="@{/static/resources/images/noimage.jpg}" id="imgx"
style="width: 210px; border: 1px dotted; margin: 5px 5px;">
</div>
<br> <br>
<div class="container text-center" role="group">
<input type="submit" value="글작성" class="btn btn-primary">
<input type="button" value="리스트" class="btn btn-danger" onclick="location='boardlist'">
</div>
</form>

이 부분에서 fb_reip를 클라이언트 IPv4 주소를 가져오고 싶은데 어떻게 해야 하나요?

 


package kr.co.trip.mvc.controller.freeboard;
 
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
 
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import kr.co.trip.mvc.dao.FreeBoardDao;
import kr.co.trip.mvc.service.FreeBoardService;
import kr.co.trip.mvc.vo.FreeBoardVO;
import kr.co.trip.mvc.vo.FreeBoard_CommVO;
 
@Controller
@RequestMapping("/board")
public class FreeboardController {
//application.properties의 key값으로 설정값을 가져와서 변수에 저장한다.
("${spring.servlet.multipart.location}")
private String filePath;
 
@Autowired
private FreeBoardService freeBoardService;
 
@RequestMapping("/boardlist")
public String fBoardList(Model model) {
List<FreeBoardVO> flist = freeBoardService.flistback();
model.addAttribute("flist", flist);
 
return "/board/bbslist";
}
 
@GetMapping("/boardwrite")
public String fboardForm() {
return "/board/bbswrite";
}
 
@PostMapping("/fboardInsert")
public String fboardInsert(Model model, @ModelAttribute("flist") FreeBoardVO vo) {
MultipartFile mf = vo.getMfile();
String oriFn = mf.getOriginalFilename();
System.out.println("oriFn : "+oriFn);
 
 
StringBuffer path = new StringBuffer(); // [서버경로]
path.append(filePath).append("\\");
path.append(oriFn);
System.out.println("FullPath : "+ path);
 
File f = new File(path.toString());
try {
mf.transferTo(f);
vo.setFb_file(oriFn);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
 
freeBoardService.fwrite(vo);
return "redirect:boardlist";
}
 
@GetMapping("/boardDetail")
public String fboardCommList(Model model, @RequestParam(required = true) int num, HttpServletRequest request) {
FreeBoardVO detail = freeBoardService.finfo(num);
List<FreeBoard_CommVO> listComm = freeBoardService.commList(num);
 
}
 
model.addAttribute("detail", detail);
model.addAttribute("listComm", listComm);
return "/board/bbdetail";
}
 
 
@GetMapping("/upHit")
public String fBoardHit(Model model, int num) {
freeBoardService.hit(num);
return "redirect:boardDetail?num=" + num;
}
 
 
 
 
 
@GetMapping("/chkPwdForm")
public String boardChkPwdForm(Model model, @RequestParam Map<String, String> param) {
System.out.println("num:" + param.get("num"));
System.out.println("job:"+param.get("job"));
model.addAttribute("map", param);
return "/board/chkpwdForm";
}
 
@GetMapping("/fbdel")
public String fboardDelete(Model model, int num, HttpSession session) {
if(session.getAttribute("sessionID") == null) {
model.addAttribute("errMsg","삭제 권한이 없습니다.");
return "/board/error";
}else {
freeBoardService.del(num);
return "redirect:boardlist";
}
 
}
 
@GetMapping("/fbmod")
public String fboardModify(Model model, int num) {
FreeBoardVO vo = freeBoardService.finfo(num);
model.addAttribute("vo", vo);
return "board/bbsmodify";
}
 
 
@PostMapping("/fbUpdate")
public String fboardUpdate(FreeBoardVO vo) {
freeBoardService.up(vo);
return "redirect:boardDetail?num="+vo.getFb_num();
}

이 소스코드에서 어떻게 하는 게 좋을까요?

 

 

 

 

 

 

 

 

 

 

 

이 질문에 댓글 쓰기 :

답변 1

getRemoteAddr() 함수를 이용하면 될것 같습니다.

@PostMapping("/fboardInsert")
public String fboardInsert(Model model, @ModelAttribute("flist") FreeBoardVO vo) {
String clientIP = null;
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
    HttpServletRequest request = attributes.getRequest();
    WebAuthenticationDetails details = (WebAuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
    clientIP = details.getRemoteAddress();
}
vo.setFb_reip(clientIP);

MultipartFile mf = vo.getMfile();

이렇게 해보세요 아래 업로드 부분은 동일 한듯 합니다.

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 3,234
© SIRSOFT
현재 페이지 제일 처음으로