Control class:
package com.example.sso.controller;
import com.example.sso.damain.UserDemo;
import com.example.sso.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
@ Controller
public class UserController {
@Autowired
private LoginService loginService;
@RequestMapping(value = "/logincheck" , method = RequestMethod.POST)
public String login(HttpServletResponse httpServletResponse,
@RequestParam(value = "UserName",required = true) String UserName,
@RequestParam(value = "PassWord",required = true) String PassWord){
System.out.println(httpServletResponse.getHeaderNames()+"hhhh");
UserDemo userDemo = new UserDemo();
userDemo.setUserName(UserName);
userDemo.setPassWord(PassWord);
if(loginService.login(userDemo,httpServletResponse).equals("SUCCESS")){
return "UserOne";
}
return "login";
}
@RequestMapping(value = "/login",method = RequestMethod.GET)
public String tologin(){
return "login";
}
}
service class:
package com.example.sso.service;
import com.example.sso.damain.UserDemo;
import org.springframework.stereotype.Service;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ Service
public class LoginService {
private static final String UserName = "root";
private static final String PassWord = "123";
public String login(UserDemo user,HttpServletResponse response){
if(user.getUserName().equals(UserName) && user.getPassWord().equals(PassWord)){
Cookie cookie = new Cookie("ssocookie","sso");
cookie.setPath("/");
response = null;
response.addCookie(cookie);
return "SUCCESS";
}
return "FAIL";
}
}