我目前正试图建立一个电子邮件注册过程与电子邮件确认。我用JavaMailSender来做这个。不幸的是,我目前收到了这个错误,所以我的应用程序不再启动。
我也非常肯定,我使用的注释是正确的。但也许我错了,因为我对斯普林布特相对来说是个新手
这是我的错误代码
Field mailSender in com.....services.EmailService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.
EmailService类
@Service
public class EmailService {
@Autowired private JavaMailSender mailSender;
public void sendVerificationEmail(User user, String siteURL)
throws MessagingException, UnsupportedEncodingException {
....
mailSender.send(message);
}
}
事后来看,我在Auth Service类中使用了该函数
@Service
public class AuthService {
....
@Autowired private EmailService emailService;
public JwtResponse signUpUser(SignUpRequest signUpRequest, String siteURL)
throws MessagingException, UnsupportedEncodingException {...
...
emailService.sendVerificationEmail(user, siteURL);
...
用户评论后的更新
创建一个EmailConfiguration类
@Configuration
public class EmailConfiguration {
@Autowired private JavaMailSender mailSender;
@Bean
public static JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername("my.gmail@gmail.com");
mailSender.setPassword("password");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return mailSender;
}
}
并将它们添加到应用程序属性中。
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
新误差
Error creating bean with name 'springSecurityFilterChain' defined in class path resource...
WebSecurityConfig类
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired private UserDetailsServiceImpl userDetailsService;
@Bean
private static JwtAuthorizationFilter authenticationJwtTokenFilter() {
return new JwtAuthorizationFilter();
}
@Bean
private static PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12);
}
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder)
throws Exception {
authenticationManagerBuilder
.userDetailsService(userDetailsService)
.passwordEncoder(WebSecurityConfig.passwordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
@Override
protected void configure(HttpSecurity http) throws Exception {
// Disable CSRF (cross site request forgery)
http.cors()
.and()
.csrf()
.disable()
// No session will be created or used by spring security
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/v1/auth/**")
.permitAll()
.anyRequest()
.authenticated();
// Apply JWT
http.addFilterBefore(
WebSecurityConfig.authenticationJwtTokenFilter(),
UsernamePasswordAuthenticationFilter.class);
}
}
发布于 2022-02-26 16:47:07
在您的application.yml中,您可以配置它(设置一些属性),并向您的maven依赖项添加spring starter。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
如文件所述:
如果spring.mail.host和相关库(由spring启动器-mail定义)可用,则在不存在的情况下创建默认的JavaMailSender。可以通过来自spring.mail命名空间的配置项进一步自定义发送方。有关更多详细信息,请参阅MailProperties。
请参阅:https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-email.html
您还可以在@Configuration带注释的类中手动设置它。参见Baeldung的示例:https://www.baeldung.com/spring-email
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername("my.gmail@gmail.com");
mailSender.setPassword("password");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return mailSender;
}
我建议您在使用spring引导时使用前者,所以让它来处理它的内容。为完整性添加第二个
看起来,依赖关系是一个问题:当您使用spring时,您可以使用它提供的基本spring设置和许多可选模块(启动器)。您不必使用这些,这就是为什么它们在默认情况下没有打开的原因。
例如,见:https://www.baeldung.com/spring-boot-starters和https://spring.io/guides/gs/securing-web/
我会列出你可以使用的依赖项以及它的用途。只要知道你不需要使用这些,它们是可选的,但是如果你要制作一个web应用程序,spring starter可能是有用的。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
类似地,如果您想要创建测试用例
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
在您的示例中,您希望使用spring安全性,因此:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
范围:测试部分是如果您使用单元测试(请参阅前面的spring starter)。如果你还没有测试,那么你可以放弃它,但是拥有它也没什么坏处。
基本上,如果您使用spring引导,并且希望从org.springframework.xxxx获得一个现成的/连接的组件,那么通常在某个地方会有一个spring starter。谷歌春季启动和软件包的名称,你会得到大量的点击。查找教程代码/初始项目代码,因为这些代码可能具有所需的依赖项。
经过一些调试后,发现剩下的问题是:在类上的工厂方法“配置”无效
@Bean
@Override
protected void configure(HttpSecurity http) throws Exception {
造成此错误的原因是@Bean建议它构造一个bean,但是它没有返回参数。(它是无效的)。配置不应该有@Bean。把它移开修好了。
https://stackoverflow.com/questions/71278456
复制相似问题