Spring Security 插入“AssignmentOperator表达式”以完成表达式

fnatzsnv  于 2023-10-20  发布在  Spring
关注(0)|答案(2)|浏览(141)

我正在沿着一本名为“学习Sping Boot 3.0第三版”的编码书,作者是Greg L。Turnquist ISBN 978-1-80323-330-7。
我在一台惠普笔记本电脑上使用Windows 11,并沿着了Spring Tool Suite 4和Java 17。
我在第4章“使用Sping Boot 保护应用程序”中遇到的问题是,代码已经弃用了SecurityFilterChain的元素。书中的原始代码有:

@Bean 
SecurityFilterChain configureSecurity(HttpSecurity http) throws Exception { 

    http
        .authorizeHttpRequests()
            .requestMatchers("/login").permitAll() 
            .requestMatchers("/", "/search").authenticated() 
            .requestMatchers(HttpMethod.GET, "/api/**").authenticated() 
            .requestMatchers("/admin").hasRole("ADMIN") 
            .requestMatchers(HttpMethod.POST, "/new-video", "/api/**").hasRole("ADMIN") 
            .anyRequest().denyAll() 
            .and()
        .formLogin() 
            .and() 
        .httpBasic(); 

    return http.build(); 
}

这段代码有很多错误,因为.authorizeHttpRequests().authorizeRequests()已经被弃用了,所以我编辑了代码:

@Bean 
SecurityFilterChain configureSecurity(HttpSecurity http) throws Exception { 

        auth-> auth.authorizeHttpRequests()
            .requestMatchers("/login").permitAll() 
            .requestMatchers("/", "/search").authenticated()  
            .requestMatchers(HttpMethod.GET, "/api/**").authenticated() 
            .requestMatchers("/admin").hasRole("ADMIN") 
            .requestMatchers(HttpMethod.POST, "/new-video" "/api/**").hasRole("ADMIN") 
            .anyRequest().denyAll() 
            .and() 
        .formLogin()
            .and() 
        .httpBasic(); 

        return http.build(); 
    } 
}

带有.httpBasic();的行显示错误:
解决错误,插入“AssignmentOperator表达式”以完成表达式
所以这就是我卡住的地方,代码中没有其他错误。其他人有解决办法吗?

6xfqseft

6xfqseft1#

下面是答案

@Bean
SecurityFilterChain configureSecurity(HttpSecurity http) throws Exception {
    http
        .authorizeRequests(authorize -> authorize
            .antMatchers("/login").permitAll()
            .antMatchers("/", "/search").authenticated()
            .antMatchers(HttpMethod.GET, "/api/**").authenticated()
            .antMatchers("/admin").hasRole("ADMIN")
            .antMatchers(HttpMethod.POST, "/new-video", "/api/**").hasRole("ADMIN")
            .anyRequest().denyAll()
        )
        .formLogin(withDefaults()) // formLogin method with default settings
        .httpBasic(withDefaults()); // httpBasic method with default settings
    return http.build();
}
niwlg2el

niwlg2el2#

你可以在下面这样使用它,它应该工作。

@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
    return http.authorizeHttpRequests((request) -> 
            request.requestMatchers("/login").permitAll()
                    .requestMatchers("/", "/search").authenticated()
                    .requestMatchers(HttpMethod.GET, "/api/**").authenticated()
                    .requestMatchers("/admin").hasRole("ADMIN")
                    .requestMatchers(HttpMethod.POST, "/new-video","/api/**").hasRole("ADMIN")
                    .anyRequest().denyAll())
            .formLogin(Customizer.withDefaults())
            .httpBasic(Customizer.withDefaults())
            .build();

}

相关问题