[英]Why authorization doesn't work and there is no access to the page?
链接我的代码。 通过Postman,我提出了用户注册请求,它出现在数据库中,一切都很好,然后在特殊选项卡“授权”中我输入Postman, Basic auth
,例如18F16EFCF42Z数据(用户名和基本密码) ,用户名:petya@mail.ru 密码:petya 请求: http://localhost:8080/landlord/1
您需要将角色从TENANT
更改为LANDLORD
。 但是我在 Postman 中得到一个错误,并且数据库中没有任何变化。 我知道授权不起作用,也许我在 SecurityConfig 文件中写错了?
<html lang = "en">
<head>
<meta charset = "utf-8">
<title> Login Customer </title>
</head>
<body>
<div class = "container">
<form class = "form-signin" method = "post" action = "/ auth / login">
<h2 class = "form-signin-heading"> Login </h2>
<p>
<label for = "username"> Username </label>
<input type = "text" id = "username" name = "username" class = "form-control" placeholder = "Username" required>
</p>
<p>
<label for = "password"> Password </label>
<input type = "password" id = "password" name = "password" class = "form-control" placeholder = "Password" required>
</p>
<button class = "btn btn-lg btn-primary btn-block" type = "submit"> Sign in </button>
</form>
</div>
</body>
</html>
安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
@Autowired
public SecurityConfig(@Qualifier("userDetailsServiceImpl") UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
/** На какие страницы человек имеет доступы */
.antMatchers("/").permitAll()
.antMatchers("/user/registration").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/auth/login").permitAll()
.defaultSuccessUrl("/auth/success")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/auth/logout", "POST"))
.invalidateHttpSession(true)
.clearAuthentication(true)
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/auth/login");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
}
@Bean
protected PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12);
}
@Bean
protected DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
daoAuthenticationProvider.setUserDetailsService(userDetailsService);
return daoAuthenticationProvider;
}
}
答:我没有这种可能,或者说没有实现,不知道能不能创造出这样的东西。 但我怀疑这是真的。
最初,选择是否要创建有或没有前,如果没有,那么你只需要 Rest 控制器(我开始这样做),你可以从这个和这个链接获得授权。 接下来,在 Postman 中,向正文发出 Post 请求 - 这将是您的授权。 !注意力! 请务必阅读我上面附加的链接上的文章,它们会详细告诉您所有内容,我这里没有完整版本的代码,只有可能有问题的那个。 还阅读那里写的评论,特别是在英语网站上,有一个关于从哪里获得“authenticationManager”的问题的答案。 我马上说,它的方法必须在SecurityConfig class中注册。
我希望我的回答能帮助某人并挽救他们的神经。
新的授权方法代码如下:
@PostMapping("/login")
public String getLoginPage(@RequestBody UserDto userDto) {
userService.loginUser(userDto);
return "login";
}
您会注意到我接受了 UserDto,其中我有:
@NotNull
@NotEmpty
private String first_name;
@NotNull
@NotEmpty
private String last_name;
@NotNull
@NotEmpty
private String password;
@NotNull
@NotEmpty
private String email;
这是授权检查本身:
public void loginUser(UserDto accountDto) {
UsernamePasswordAuthenticationToken authReq
= new UsernamePasswordAuthenticationToken(accountDto.getEmail(), accountDto.getPassword());
Authentication auth = authenticationManager.authenticate(authReq);
SecurityContext sc = SecurityContextHolder.getContext();
sc.setAuthentication(auth);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.