in.hocg.sso.server.sample.config.security.IndexEndpoint Maven / Gradle / Ivy
package in.hocg.sso.server.sample.config.security;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.Objects;
/**
* Created by hocgin on 2020/1/6.
* email: [email protected]
*
* @author hocgin
*/
@Slf4j
@Controller
@RequestMapping
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class IndexEndpoint {
@RequestMapping("/signup")
public ModelAndView signup() {
return new ModelAndView("signup");
}
@RequestMapping("/login")
public ModelAndView login() {
return new ModelAndView("login");
}
@GetMapping({"/", "/index.html", "/index"})
public ModelAndView index() {
return new ModelAndView("index");
}
@RequestMapping("/oauth/user")
@ResponseBody
public Principal user(Principal principal) {
return principal;
}
@RequestMapping("/logout")
@ResponseBody
public String logout(HttpServletRequest request, HttpServletResponse response) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (Objects.nonNull(authentication)) {
new SecurityContextLogoutHandler().logout(request, response, authentication);
}
return "logout";
}
}