【问题标题】:Why authorization doesn't work and there is no access to the page?为什么授权不起作用并且无法访问页面?
【发布时间】:2020-12-01 01:54:13
【问题描述】:

Link我的代码。通过Postman,我请求用户注册,出现在数据库中,一切正常,然后在我进入Postman的特殊标签“授权”中,选择Basic auth,输入数据(用户名和密码),为例如,用户名: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;
    }
}

【问题讨论】:

    标签: java spring-boot hibernate postman


    【解决方案1】:

    Answer:我没有这种可能,或者说没有实现,不知道能不能创造出这样的东西。但我怀疑这是真的。

    最初,选择是否要创建有或没有前,如果没有,那么你只需要休息控制器(我开始这样做),你可以从thisthis链接获得授权。接下来,在 Postman 中,向正文发出 Post 请求 - 这将是您的授权。 !注意! 请务必阅读我上面附加的链接上的文章,它们会详细告诉您所有内容,我这里没有完整版本的代码,只有可能有问题的那个.还阅读那里写的 cmets,尤其是在英语网站上,有一个关于从哪里获得“authenticationManager”的问题的答案。我马上说,它的方法必须在SecurityConfig类中注册。

    我希望我的回答能帮助别人并拯救他们的神经。

    新的授权方式代码如下:

    @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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2015-05-24
      • 2021-10-22
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多