当前位置: 首页 > 知识库问答 >
问题:

我可以添加一个优先级来执行方法级别@bean吗?

艾焱
2023-03-14

在我的测试应用程序中,我有我的安全配置类,如下所示。

package myProject.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@SuppressWarnings("deprecation")
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userService;

    @Override
    public void configure(HttpSecurity theHttp) throws Exception {
        theHttp.authorizeRequests()
                .antMatchers("/design", "/order")
                    .access("hasRole('ROLE_USER')")
                .antMatchers("/", "/**")
                    .access("permitAll")
                .and()
                .formLogin()
                    .loginPage("/login")
                    .defaultSuccessUrl("/", true) 
                .and()
                .logout()
                    .logoutSuccessUrl("/login")
                .and()
                .csrf()
                    .ignoringAntMatchers("/h2-console/**")
                .and()
                .headers()
                    .frameOptions()
                    .sameOrigin();
    }

    @Bean
    public PasswordEncoder encoder() {
        return new StandardPasswordEncoder("53cr3t");
    }

    @Override
    public void configure(AuthenticationManagerBuilder theAuth) throws Exception {
        theAuth.userDetailsService(userService)
                .passwordEncoder(encoder());
    }

}

在那里我定义了encoder()并将其注释为@beans。这意味着该方法生成一个bean由spring容器管理。同样,我需要通过构造函数访问编码器,如下所示。

package myProject.security;

import org.springframework.context.annotation.DependsOn;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import myProject.data.UserRepository;

@Controller
@RequestMapping("/register")
public class RegistrationController {

    private final UserRepository userRepo;
    private final PasswordEncoder passwordEncoder;

    public RegistrationController(UserRepository theUserRepo, PasswordEncoder thePasswordEncoder) {
        this.userRepo = theUserRepo;
        this.passwordEncoder = thePasswordEncoder;
    }

    @GetMapping
    public String registrationForm() {
        return "userRegistry";
    }

    @PostMapping
    public String processRegistration(RegistrationForm theUserRegistration) {
        userRepo.save(theUserRegistration.tranformUser(passwordEncoder));
        return "redirect:/loginPage";
    }

}

在上面的示例中,我使用密码编码器作为构造函数参数。问题是@Bean方法不是由调用RegistrationController的时间构造函数执行的。在将encoder()添加到bootstrap类之后,我可以克服这个问题,但我认为这不是一个解决方案。谁能帮我想办法解决我的问题。事先谢谢。

共有1个答案

左丘成业
2023-03-14

请将@bean方法的名称更改为passwordencoder()

@Bean
    public PasswordEncoder passwordEncoder () {
        return new StandardPasswordEncoder("53cr3t");
}

默认情况下,配置类使用@bean方法的名称作为生成bean的名称。但是,可以使用name属性重写此功能。

调用顺序是可以的,但是您没有名为passwordEncoder的bean

 类似资料:
  • 问题内容: 我正在创建一些使用ajax来获取页面信息的插件。现在有不同的插件执行不同的功能。 所有插件都需要独立使用,但是我还想添加一些功能,使其可以很好地协同工作。 所有不同的插件都有自己的JavaScript文件和功能。就HTTP请求而言,这是没有问题的,因为大多数人都有插件,这些插件可以在网站打开时将所有文件合并为一个。 - 现在我的问题。正如我所说的,不同的插件会在网站的不同部分产生作用。

  • 我有log4j2.xml,它部分配置为: 但是,跟踪消息并不存在于两个文件附加符(fileinfo/filedebug)中。当我更改FileDebug log level=“TRACE”时,就会出现跟踪消息。

  • 问题内容: 我按以下顺序设置了线程的优先级 先是A然后是B,然后是C。但是当我在下面的程序中运行时,有时B在A之前运行。我不理解这种执行方式,因为我将B的优先级设置为小于A的优先级。 } 问题答案: 线程优先级可能不是您认为的那样。 线程的优先级是对操作系统的建议,在涉及这两个线程的任何调度或CPU分配决策点中,一个线程优先于另一个线程。但是,如何实现这一点取决于操作系统和JVM的实现。 Java

  • 我有一个(我希望)简单的问题要问那些有就餐交响乐经验的人。 基于注释的调度允许设置优先级。如果我为此使用了SchduleParameters.FIRST_PRIORITY和SchduleParameters.LAST_PRIORITY参数,如果每个代理在每个滴答处执行这些方法,则整体调度程序如何解释这一点? > 首先,所有代理都使用ScheduleParameters执行该方法。首先是优先级,然后

  • 我正在使用std::priority\u队列和std::vector中的一些自定义对象。现在假设在调用top()函数时,有具有相同优先级的对象,我会按从最旧到最新的顺序获取它们。那么我的问题是,有没有可能改变这种行为,以便top()在优先级相同的情况下返回最近的对象?

  • 我有一个模型,大约有10种调度方法。现在我对控制它们的执行有点困惑。我希望这些调度方法按照一定的顺序执行。 我怎么能有ScheduleParameters。第一优先级,计划参数。第二优先级,调度参数。第三优先权。。。,和调度参数。最后一个优先事项。