注解标红,springframework.

您需要引入以下的包: ```java import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.http.HttpMethod; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.beans.factory.annotation.Autowired; ``` 然后,您需要将`SecurityConfig`类继承自`WebSecurityConfigurerAdapter`类。在类上添加`@Configuration`和`@EnableWebSecurity`注解。 接下来,您需要重写`configure(HttpSecurity http)`方法。您可以使用`.csrf().disable()`禁用CSRF保护,`.authorizeRequests().antMatchers("/api/**").authenticated()`配置只有经过身份验证的用户才能访问您的API。最后,您需要使用`apply()`方法将您的`JwtConfigurer`类应用于HttpSecurity。 然后,您需要重写`configure(WebSecurity web)`方法并忽略OPTIONS请求。 最后,您需要创建一个名为`passwordEncoder()`的方法,该方法返回一个`BCryptPasswordEncoder`实例。这个实例将用于对密码进行加密。 以下是修改后的代码: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private JwtTokenProvider jwtTokenProvider; @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests() .antMatchers("/api/**").authenticated() .and() .apply(new JwtConfigurer(jwtTokenProvider)); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**"); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(12); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值