我正试图通过Zuul代理从Spring Cloud Oauth2服务中获取访问令牌,我已经在javafx上成功实现了该令牌,而且使用邮递员也很好。
现在,我正在尝试使用html和javascript达到相同的结果。
这是我到目前为止所做的:
我的login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>OAuth2 SSO Demo</title>
<meta http-equiv='cache-control' content='no-cache'/>
<meta http-equiv='expires' content='0'/>
<meta http-equiv='pragma' content='no-cache'/>
<meta name="viewport" content="width=device-width, initial-scale=0.8"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1>Login Form</h1>
<div name="loginForm" >
<input type="text" placeholder="Username" id="username" name="username" value="user"/><br />
<input type="password" placeholder="Password" id="password" name="password" value="password"/><br />
<button id="logginButton" onClick="submitForm()">Login</button><br />
</div>
<script type="text/javascript" src="js/Login.js"></script>
</body>
</html>
js如下(从邮递员生成):
function submitForm() {
alert("message");
var form = new FormData();
form.append("grant_type", "password");
form.append("username", $("#username").val());
form.append("password", SHA256($("#password").val()));
form.append("client_id", "mds_group");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:9999/uaa/oauth/token",
"method": "POST",
"headers": {
"authorization": "Basic bWRzX2dyb3VwOm1kc19ncm91cA==",
"cache-control": "no-cache",
"postman-token": "31e63c4d-32f1-a941-3f0f-87d9a22a8c91"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function(response) {
console.log(response);
});
}
我的Spring Boot UAA配置是:
@SpringBootApplication
@EnableAuthorizationServer
@EnableEurekaClient
@RestController
@SessionAttributes("authorizationRequest")
public class AuthorizationApplication {
public static void main(String[] args) {
SpringApplication.run(AuthorizationApplication.class, args);
}
@Configuration
static class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("login").setViewName("login");
registry.addViewController("/oauth/confirm_access").setViewName("authorize");
registry.addViewController("/").setViewName("index");
}
}
@Configuration
static class LoginConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.getWriter().write("success 200");
response.getWriter().append("\n welcome to mds microservice eco system");
}
}).failureHandler(new AuthenticationFailureHandler() {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
response.getWriter().write("failure 400 Bad Credentials"+exception.getMessage());
}
}).and().logout().logoutUrl("/logout")
.and()
.requestMatchers()
.antMatchers("/", "/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and().httpBasic().and().csrf().disable();
}
@Autowired
MDSUserDetailService mdsUserServiceDetail;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(mdsUserServiceDetail);
}
}
@Profile("!cloud")
@Bean
RequestDumperFilter requestDumperFilter() {
return new RequestDumperFilter();
}
}
我在chrome控制台中遇到的异常是
无法加载资源:服务器响应状态为401() 无法加载http://localhost:9999/uaa/oauth/token:飞行前响应没有HTTP正常状态。
我尝试了此解决方案,但无效:1