Я пытаюсь создать форму для входа, используя Spring Boot MVC. После отправки из формы входа в систему как POST на URL-адрес аутентификации, когда я возвращаю объект ModelAndView, он пытается сделать вызов POST для View вместо вызова GET. Вот некоторые фрагменты кода:
По сути, я был тем, что сервлет-диспетчер должен делать вызов FORWARD GET вместо FORWARD POST на home. html
Login. HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.js"></script>
<link rel="stylesheet" type="text/css" href="login.css" />
</head>
<body data-ng-app="">
<div id="outer_div">
<!-- <div data-ng-include="'header.html'"></div>-->
<header id="top_header"> Welcome to Online Notes Portal </header>
<div class="content">
<!-- <div data-ng-include="'LHN.html'"></div>-->
<div id="main_content">
<form enctype="application/json" class="form">
<input type="text" id="username" value="username"></input>
<input type="text" id="password" value="password"></input>
<input type="button" onclick="sub()" id="Submit_btn" value="Login"></input>
</form>
</div>
</div>
<div data-ng-include="'/secure/footer.html'"></div>
</div>
<script type="text/javascript">
function sub(){
var obj = new Object();
var http = new XMLHttpRequest();
obj.username = document.getElementById("username").value;
obj.password = document.getElementById("password").value;
var jsonString = JSON.stringify(obj);
http.open("POST", "http://localhost:8080/notes/do_login", false);
http.setRequestHeader("Content-type", "application/json");
http.send(jsonString);
console.log(jsonString);
document.write(http.responseText);
/*
document.getElementById("Username").value = http.responseText;
http.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("UserName").value = this.responseText;
}
} */
}
</script>
</body>
</html>
<style>
#top_header{
background: var(--back);
font: bold 30px Tahoma cursive;
/* background-color: green; */
text-align: center;
padding: 10px;
}
</style>
application.properties
logging.level.org.springframework.security=TRACE
logging.level.org.springframework=TRACE
Java Метод:
@RequestMapping(value = "/notes/do_login", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ModelAndView performLogin(@RequestBody UserDetails userDetails) {
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println(userDetails);
//System.out.println(userDetails);
ModelAndView modelAndView = new ModelAndView();
ResponseEntity response = isValidCred(userDetails);
if(((String)response.getBody()).equalsIgnoreCase("yes")){
loggedUser.setUsername(userDetails.getUsername());
modelAndView.setViewName("/secure/home.html");
modelAndView.setStatus(HttpStatus.OK);
} else {
modelAndView.setViewName("/unsecure/error.html");
modelAndView.setStatus(HttpStatus.UNAUTHORIZED);
}
return modelAndView;
}
Основной класс:
@SpringBootApplication(scanBasePackages = "com.notes"
,exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class}
)
public class NotesApplication {
public static void main(String[] args) {
SpringApplication.run(NotesApplication.class, args);
}
}
Журналы:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UserDetails(username=user1, password=user123, role=null)
user :UserDetails(username=user1, password=user123, role=null)
hash : 95c946bf622ef93b0a211cd0fd028dfdfcf7e39e
2020-05-02 08:14:07.335 TRACE 15756 --- [nio-8080-exec-2] o.s.b.f.s.DefaultListableBeanFactory : Invoking afterPropertiesSet() on bean with name '/secure/home.html'
2020-05-02 08:14:07.335 DEBUG 15756 --- [nio-8080-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected '*/*' given [*/*]
2020-05-02 08:14:07.335 TRACE 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name '/secure/home.html'; URL [/secure/home.html]]
2020-05-02 08:14:07.335 DEBUG 15756 --- [nio-8080-exec-2] o.s.w.servlet.view.InternalResourceView : View name '/secure/home.html', model {org.springframework.validation.BindingResult.userDetails=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
2020-05-02 08:14:07.336 DEBUG 15756 --- [nio-8080-exec-2] o.s.w.servlet.view.InternalResourceView : Forwarding to [/secure/home.html]
2020-05-02 08:14:07.338 TRACE 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for POST "/secure/home.html", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2020-05-02 08:14:07.339 TRACE 15756 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 4 interceptors
2020-05-02 08:14:07.343 WARN 15756 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
2020-05-02 08:14:07.343 TRACE 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : No view rendering, null ModelAndView returned.
2020-05-02 08:14:07.343 DEBUG 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 405, headers={masked}
2020-05-02 08:14:07.343 DEBUG 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 405 METHOD_NOT_ALLOWED, headers={masked}
2020-05-02 08:14:07.343 TRACE 15756 --- [nio-8080-exec-2] o.s.b.w.s.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@28fde84b
2020-05-02 08:14:07.344 TRACE 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2020-05-02 08:14:07.344 TRACE 15756 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 matching mappings: [{ /error}, { /error, produces [text/html]}]
2020-05-02 08:14:07.344 TRACE 15756 --- [nio-8080-exec-2] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'basicErrorController'
2020-05-02 08:14:07.344 TRACE 15756 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2020-05-02 08:14:07.345 TRACE 15756 --- [nio-8080-exec-2] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: [org.apache.catalina.core.ApplicationHttpRequest@7686f461]
2020-05-02 08:14:07.348 DEBUG 15756 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2020-05-02 08:14:07.348 TRACE 15756 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Sat May 02 08:14:07 IST 2020, status=405, error=Method Not Allowed, message=Request method 'POST' not supported, path=/notes/do_login}]
2020-05-02 08:14:07.356 TRACE 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : No view rendering, null ModelAndView returned.
2020-05-02 08:14:07.356 DEBUG 15756 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 405, headers={masked}