簡體   English   中英

為什么授權不起作用並且無法訪問頁面?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM