我一直在尝试正确配置此配置,但是我遇到了一些困难,我相信这是一个我没有看到的小修复。默认的 spring security 登录页面工作得很好,但我无法渲染自定义 html。它只是将获取映射“login”读取为字符串。任何帮助将不胜感激!
1.安全配置
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsService myUserDetailsServices;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailsServices);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/user").hasAnyRole("ADMIN", "USER")
.antMatchers("/").permitAll()
.and().formLogin()
.loginPage("/login")
.permitAll();
}
@Bean
public PasswordEncoder getPasswordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
}
3.主控制器
@RestController
@CrossOrigin(origins="http://localhost:4200")
public class MainController {
private UserRepository userRepository;
private UserDetailsService myUserDetailsServices;
public MainController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/")
public String home() { return ("<h1>Welcome</h1>");
}
@GetMapping("/login")
public String login() { return ("login");
}
@GetMapping("/user")
public String user() {
return ("<h1>Welcome User</h1>");
}
@GetMapping("/admin")
public String admin() {
return ("<h1>Welcome Admin</h1>");
}
@GetMapping("/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
慕后森
慕斯709654
蛊毒传说
相关分类