创建名称为“org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration”的bean时出错Injectionofdependencyfailed
温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
Creating a basic Spring Security project for learning purposes by using following dependencies web, lombock, validation, h2, jpa, oauth2, configuration-processor.
While running the project I am getting following error in the console. Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed
Below are the required code for the Controller and other required files.
Controller
package controller;
import lombok.RequiredArgsConstructor;
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class DashboardController {
@PreAuthorize("hasAnyRole('ROLE_MANAGER','ROLE_ADMIN','ROLE_USER')")
@GetMapping("/welcome-message")
public ResponseEntity<String> getFirstWelcomeMessage(Authentication authentication){
return ResponseEntity.ok("Welcome to the JWT Tutorial:"+authentication.name()+"with scope:"+authentication.getClass());
}
@PreAuthorize("hasRole('ROLE_MANAGER')")
@GetMapping("/manager-message")
public ResponseEntity<String> getManagerData(Principal principal){
return ResponseEntity.ok("Manager::"+principal.getName());
}
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/admin-message")
public ResponseEntity<String> getAdminData(@RequestParam("message") String message, Principal principal){
return ResponseEntity.ok("Admin::"+principal.getName()+" has this message:"+message);
}
}
SecurityConfig File
package config;
import config.userConfig.UserInfoManagerConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import static org.springframework.security.config.Customizer.withDefaults;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserInfoManagerConfig userInfoManagerConfig;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated();
// Disable CSRF (Cross-Site Request Forgery) for H2 console
http.csrf().ignoringAntMatchers("/h2-console/**");
// Allow framing of H2 console
http.headers().frameOptions().sameOrigin();
}
@Order(1)
@Bean
public SecurityFilterChain apiSecurityFilterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.requestMatcher(new AntPathRequestMatcher("/api/**"))
.csrf(AbstractHttpConfigurer::disable)
.authorizeRequests(auth -> auth.anyRequest().authenticated())
.userDetailsService(userInfoManagerConfig)
.formLogin(withDefaults())
.httpBasic(withDefaults());
return httpSecurity.build();
}
@Order(2)
@Bean
public SecurityFilterChain h2ConsoleSecurityFilterChainConfig(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.requestMatchers()
.antMatchers("/h2-console/**")
.and()
.authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.and()
.csrf()
.ignoringAntMatchers("/h2-console/**")
.and()
.headers()
.frameOptions().disable();
return httpSecurity.build();
}
}
Application.yml
spring:
h2:
console:
enabled: true
datasource:
url: jdbc:h2:mem:atquilDB
username: sa
password: sa
driverClassName: org.h2.Driver
jpa:
spring.jpa.database-platform: org.hibernate.dialect.H2Dialect
show-sql: true
hibernate:
ddl-auto: create-drop
logging:
level:
org.springframework.security: trace
I am using JPA for the interaction with H2 database, here is the Repo class details
package repo;
import entity.UserInfoEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface UserInfoRepo extends JpaRepository<UserInfoEntity,Long> {
Optional<UserInfoEntity> findByEmailId(String emailId);
}
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2419
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权