You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.8 KiB
38 lines
1.8 KiB
package cc.bnblogs.springsecurity.controller; |
|
|
|
import org.springframework.http.HttpStatus; |
|
import org.springframework.security.web.DefaultRedirectStrategy; |
|
import org.springframework.security.web.RedirectStrategy; |
|
import org.springframework.security.web.savedrequest.HttpSessionRequestCache; |
|
import org.springframework.security.web.savedrequest.RequestCache; |
|
import org.springframework.security.web.savedrequest.SavedRequest; |
|
import org.springframework.util.StringUtils; |
|
import org.springframework.web.bind.annotation.GetMapping; |
|
import org.springframework.web.bind.annotation.ResponseStatus; |
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.IOException; |
|
|
|
@RestController |
|
public class BrowserSecurityController { |
|
private final RequestCache requestCache = new HttpSessionRequestCache(); |
|
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); |
|
|
|
@GetMapping("/authentication/require") |
|
@ResponseStatus(HttpStatus.UNAUTHORIZED) |
|
public String requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException { |
|
SavedRequest savedRequest = requestCache.getRequest(request,response); |
|
if (savedRequest != null) { |
|
String targetUrl = savedRequest.getRedirectUrl(); |
|
if (StringUtils.endsWithIgnoreCase(targetUrl,".html")) { |
|
redirectStrategy.sendRedirect(request,response,"/login.html"); |
|
} |
|
if (StringUtils.pathEquals(targetUrl,"http://localhost:8080/login")) { |
|
redirectStrategy.sendRedirect(request,response,"/login.html"); |
|
} |
|
} |
|
return "您访问的资源需要身份认证!"; |
|
} |
|
}
|
|
|