空白-checkmate 2024-02-09 11:59 采纳率: 33.3%
浏览 3

springboot

只有放行 /**的时候 /user/account/token/才可以访问


 
@Configuration
@EnableWebSecurity
public class SecurityConfig {
 
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
 
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
 
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
        return authConfig.getAuthenticationManager();
    }
 
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf(CsrfConfigurer::disable) // 基于token,不需要csrf
                .sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // 基于token,不需要session
                .authorizeHttpRequests((authz) -> authz
                        .requestMatchers("/user/account/token/").permitAll() // 放行api
                        .requestMatchers(HttpMethod.OPTIONS).permitAll()
                        .anyRequest().authenticated()
                )
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
 
        return http.build();
    }
}
 
 
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/user/account/token/")
    public Map<String, String> getToken(@RequestParam Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");
 
        return loginService.getToken(username, password);
    }
 
}

  • 写回答

3条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2024-02-09 14:04
    关注

    【以下回答由 GPT 生成】

    问题标题: Spring Boot应用中如何配置只有放行 / 的时候 /user/account/token/ 才可以访问?**


    问题描述:

    我想在Spring Boot应用中配置一个只有当路径以 /user/account/token/ 开头时,才允许访问。其他路径都需要进行身份验证和权限控制。请问如何实现这个需求?


    解决方案:

    在Spring Boot中,可以使用Spring Security来进行身份验证和权限控制。下面是具体的解决方案步骤:

    1. 添加Spring Security依赖到项目的pom.xml文件中:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
    1. 创建一个配置类,并在该配置类中配置Spring Security,例如,创建一个名为 SecurityConfig 的类,并添加注解 @EnableWebSecurity@Configuration
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        // 配置相关信息
    }
    
    1. 在配置类中覆盖 configure(HttpSecurity http) 方法,来配置安全规则。在该方法中,可以使用 antMatchers 方法来配置针对特定路径的安全规则:
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/user/account/token/**").permitAll() // 放行 /user/account/token/ 路径
                .anyRequest().authenticated() // 其他路径需要进行身份验证
                .and()
            .formLogin()
                .and()
            .httpBasic();
    }
    
    1. 配置完成后,启动应用程序并进行访问,当路径以 /user/account/token/ 开头时,将会被放行,其他路径将需要进行身份验证。

    这就是解决该问题的具体步骤和代码示例。如果你有任何问题,请随时询问。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 2月9日

悬赏问题

  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画
  • ¥15 hadoop中启动hive报错如下怎么解决
  • ¥15 如何优化QWebEngineView 加载url的速度
  • ¥15 关于#hadoop#的问题,请各位专家解答!
  • ¥15 如何批量抓取网站信息