尝试了所有可能的方法,但每个方法都有不同的答案。我尝试从login.html页面登录并转到notes.html,但浏览器返回405错误。我用Spring Boot2.1.1; java 8;mysql数据库;百里香
问题:
以下是我的层次结构:
这是我的安全配置:
package com.dzeru.springloginformandoauth2tutorial.config;
import com.dzeru.springloginformandoauth2tutorial.repos.UserRepo;
import com.dzeru.springloginformandoauth2tutorial.services.AuthProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
@Configuration
@EnableWebSecurity
@EnableOAuth2Client
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthProvider authProvider;
@Autowired
private PasswordEncoder passwordEncoder;
@Qualifier("oauth2ClientContext")
@Autowired
private OAuth2ClientContext oAuth2ClientContext;
@Autowired
private UserRepo userRepo;
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
@ConfigurationProperties("google.client")
public AuthorizationCodeResourceDetails google()
{
return new AuthorizationCodeResourceDetails();
}
@Bean
@ConfigurationProperties("google.resource")
public ResourceServerProperties googleResource()
{
return new ResourceServerProperties();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**").anyRequest();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers( "/login**", "/registration", "/resources/**", "/registration", "/").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login")
.defaultSuccessUrl("/notes").failureUrl("/login?error").permitAll()
.and().logout().logoutSuccessUrl("/").permitAll();
http
.authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
http
.csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(authProvider);
}
}
这是我的注册控制器:
package com.dzeru.springloginformandoauth2tutorial.controllers;
import com.dzeru.springloginformandoauth2tutorial.entities.Role;
import com.dzeru.springloginformandoauth2tutorial.entities.User;
import com.dzeru.springloginformandoauth2tutorial.repos.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
@Controller
public class RegistrationController
{
@Autowired
private UserRepo userRepo;
@Autowired
private PasswordEncoder passwordEncoder;
@GetMapping("/registration")
public String registration()
{
return "registration";
}
@PostMapping("/registration")
public String addUser(String name, String username, String password) {
User user = new User();
user.setName(name);
user.setUsername(username);
user.setPassword(passwordEncoder.encode(password));
user.setActive(true);
user.setRoles(Collections.singleton(Role.USER));
userRepo.save(user);
return "redirect:/login";
}
}
和login.html:
<!DOCTYPE html>
<html lang="uk" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" th:href="@{/css/login.css}" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>YKW</title>
<link rel="shortcut icon" th:href="@{img/favicon.ico}" type="image/x-icon">
</head>
<body>
<form action="/login" class="auto_form" method="post">
<div class="container">
<label><b>Username</b></label>
<label>
<input type="text" id="email" placeholder="Enter Username" name="username" required>
</label>
<label><b>Password</b></label>
<label>
<input type="password" id="password" placeholder="Enter Password" name="password" required>
</label>
<label>
<select class="selector">
<option>Male</option>
<option>Female</option>
<option>None</option>
<option>IDK</option>
<option>Agender</option>
<option>Androgyne</option>
<option>Androgynous</option>
<option>Bigender</option>
<option>Cis</option>
<option>Cisgender</option>
<option>Cis Female</option>
<option>Cis Male</option>
<option>Cis Man</option>
<option>Cis Woman</option>
<option>Cisgender Female</option>
<option>Female to Male</option>
<option>FTM</option>
<option>Gender Fluid</option>
<option>Gender Nonconforming</option>
<option>Gender Questioning</option>
<option>Gender Variant</option>
<option>Genderqueer</option>
<option>Intersex</option>
<option>Male to Female</option>
<option>MTF</option>
<option>Other</option>
<option>Neither</option>
<option>Transsexual Person</option>
<option>Transsexual Man</option>
<option>Transsexual Person</option>
<option>Anarcho-Communist</option>
<option>I don`t know</option>
</select>
</label>
<button type="submit" class="button_login">Login</button>
<a href="/registration"><button type="button" class="button_register">Registration</button></a>
</div>
</form>
<script>
console.log(" _ \n" +
" | \\ _ ._ | _ _ _| |_ \\_/ |/ \n" +
" |_/ (/_ |_) | (_) \\/ (/_ (_| |_) \\/ | |\\ \n" +
" | / / ");
</script>
</body>
</html>
暂无答案!
目前还没有任何答案,快来回答吧!