spring 相关问题

Spring Framework是Java平台上应用程序开发的开源框架。其核心是对基于组件的体系结构的丰富支持,目前它拥有20多个高度集成的模块。

Spring Boot 配置值带有 .no。或没有。未填充

我有一个非常简单的 Spring Boot 应用程序,具有特定于国家/地区的配置属性,所有已知且简单的东西,没有什么特别的。 它应该在 DK、SE、FI 和 NO 4 个国家/地区工作。注射这些正确的...

回答 1 投票 0

从 RestTemplate 迁移到 WebFlux

我有这个 RestTemplate,我想将其迁移到 WebFlux 客户端: RestTemplaterestTemplate = new RestTemplate(); 字符串 url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml&...

回答 1 投票 0

当我在代码中使用 thymeleaf 并将标头代码替换为单独的文件时,然后不显示标头选项卡

我正在学习 Spring Boot 并在我的项目中使用 Thymeleaf。当我将标头代码替换为同一文件夹中的单独文件并使用 Thymeleaf 的替换语法、CSS、JS 和 Bootstrap 代码时...

回答 1 投票 0

如何在Spring集成中向流程发送消息?

假设以下流程: 公共 IntegrationFlow sendFlow() { 返回 IntegrationFlow.from(inputChannel()) .handle("某事","someMethod") ...

回答 1 投票 0

无法在我的eclipse中安装spring,不想安装springboot,我需要使用spring core

我正在尝试从eclipse市场在eclipse中安装spring工具套件,但是这个安装过程已完成,但最终无法安装,就像显示的对话框一样,还显示了一些

回答 1 投票 0


我无法获得授权,我需要注册

导入java.util.可选; 导入 org.springframework.data.jpa.repository.JpaRepository; 导入 com.example.secure.models.User; 公共接口 UserRepository 扩展了 JpaRepository import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import com.example.secure.models.User; public interface UserRepository extends JpaRepository<User,Integer>{ Optional<User> findAppUserByUsername(String username); } import com.example.secure.repository.UserRepository; import com.example.secure.models.User; import org.springframework.stereotype.Service; @Service public class UserDetailsServiceImpl implements UserDetailsService { private final UserRepository appUserRepository; public UserDetailsServiceImpl(UserRepository appUserRepository) { this.appUserRepository = appUserRepository; } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = appUserRepository .findAppUserByUsername(username) .orElseThrow(() -> new UsernameNotFoundException("Not found")); return org.springframework.security.core.userdetails.User.withUsername(user.getUsername()) .password(user.getPassword()) .roles(user.getAuthority()) .build(); } } @Entity public class User{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String username; private String password; private String authority; //constructors setters and getters } import java.util.Collection; import java.util.List; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; public class UserAdapter implements UserDetails { private User user; public UserAdapter(User user){ this.user=user; } @Override public Collection<? extends GrantedAuthority> getAuthorities() { return List.of(new SimpleGrantedAuthority(user.getAuthority())); } @Override public String getPassword() { return user.getPassword(); } @Override public String getUsername() { return user.getUsername(); } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return true; } } import com.example.secure.models.User; import com.example.secure.repository.UserRepository; @RestController public class SecurityController { private final UserRepository repo; private final PasswordEncoder passwordEncoder; public SecurityController(UserRepository repo, PasswordEncoder passwordEncoder) { this.repo = repo; this.passwordEncoder = passwordEncoder; } @PostMapping(path = "/register") public String register(@RequestBody RegistrationRequest request) { User user = new User(); System.out.println("Controller started"); user.setUsername(request.username()); user.setPassword(passwordEncoder.encode(request.password())); user.setAuthority(request.authority()); System.out.println(user.toString()); repo.save(user); System.out.println("Controller ended"); return "New user successfully registered"; } @GetMapping(path = "/test") public String test() { return "Access to '/test' granted"; } record RegistrationRequest(String username, String password, String authority) { } } import com.example.secure.service.UserDetailsServiceImpl; @Configuration @EnableWebSecurity(debug = true) public class SecurityConfig { private UserDetailsServiceImpl userDetailsService; public SecurityConfig(UserDetailsServiceImpl userDetailsService) { this.userDetailsService = userDetailsService; } @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .csrf(configurer -> configurer.disable()) .authorizeHttpRequests(auth -> auth .requestMatchers(HttpMethod.GET, "/test").hasAuthority("ROLE_USER") .requestMatchers(HttpMethod.POST, "/register").permitAll() .anyRequest().denyAll()) .httpBasic(Customizer.withDefaults()) .userDetailsService(userDetailsService); return http.build(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } 以上是我的项目相关的Spring Security代码,当我尝试使用Postman访问端点时,该代码不起作用。当我从 Postman 调用“est”端点时,会从数据库中获取详细信息,但我无法访问该端点。我收到 401 状态代码,如图所示。来自邮递员的 401 状态代码显示响应标头的图像 我从过去两天开始尝试,但无法得到它。 我预计问题是什么以及如何解决。 您有 UserAdapter 类,但实现不完整。请使用以下 UserAdapter 类。 loadUserByUsername():这个抽象方法将返回 UserDetails 类实例,这就是为什么我们必须使用 UserAdapter 类来实现 UserDetails public class UserAdapter implements UserDetails { private String username; private String password; private List<GrantedAuthority> authorities; public UserAdapter(User user){ this.username = user.getUsername(); this.password = user.getPassword(); this.authorities = Stream.of(user.getAuthority().split(",")).map(SimpleGrantedAuthority::new).collect( Collectors.toList()); } @Override public Collection<? extends GrantedAuthority> getAuthorities() { return this.authorities; } @Override public String getPassword() { return this.password; } @Override public String getUsername() { return this.username; } @Override public boolean isAccountNonExpired() { return false; } @Override public boolean isAccountNonLocked() { return false; } @Override public boolean isCredentialsNonExpired() { return false; } @Override public boolean isEnabled() { return false; } } 接下来创建一个单独的类并实现 AuthenticationProvider,因此此实现将帮助提供商管理器识别此客户身份验证 @Component public class CustomizeAuthProvider implements AuthenticationProvider { private final UserDetailsService userDetailsService; public CustomizeAuthProvider(UserDetailsService userDetailsService) { this.userDetailsService = userDetailsService; } @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { try{ UserDetails userDetails = userDetailsService.loadUserByUsername(authentication.getName()); return new UsernamePasswordAuthenticationToken(userDetails.getUsername(),userDetails.getPassword(),userDetails.getAuthorities()); }catch (UsernameNotFoundException e){ throw new BadCredentialsException("Credentials is invalid"); } } @Override public boolean supports(Class<?> authentication) { return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); } } 替换 UserDetailsServiceImpl 类中的 loadUserByserName 方法 @Override public UserAdapter loadUserByUsername(String username) throws UsernameNotFoundException { Optional<User> user = appUserRepository.findAppUserByUsername(username); return user.map(UserAdapter::new).orElseThrow(()->new UsernameNotFoundException("Not found")); } 还用以下代码更新 securityConfig 方法 @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(auth -> auth .requestMatchers(HttpMethod.GET, "/test").hasAuthority("ROLE_USER") .requestMatchers(HttpMethod.POST, "/register").permitAll() .anyRequest().denyAll()) .httpBasic(Customizer.withDefaults()) //here we are providing customer authentication provider .authenticationProvider(authenticationProvider) .userDetailsService(userDetailsService); return http.build(); } 希望这对您有帮助

回答 1 投票 0

Spring-data-mongo 无法使用构造函数实例化 java.util.List

使用 spring-data-mongodb-1.5.4 和 mongodb-driver-3.4.2 我有一个等级酒店 公共类酒店{ 私有字符串名称; 私有 int 价格每晚; 私人地址地址...

回答 3 投票 0

替代Kafka 3.6.2中的@EnableBinding和@StreamListener

我一直在致力于从 Spring 2.X 到 3.X 的迁移,我的旧代码在消费者中使用了 @EnableBinding(Sink::class) 和 @StreamListener(Sink.INPUT) 。两者都已被弃用并从...

回答 1 投票 0

Spring Data JPA 规范组By

对不起我的英语首先。 我想使用jpa进行groupby,例如:从数据流组中按scrip、dstip选择scrip、dustup、count(*)。 所以,编写这些代码: 公共类数据流规范{ 公共站...

回答 4 投票 0

@PreAuthorize(permitAll) 仍然需要身份验证

我的存储库中有以下示例方法(带有 @RepositoryRestResource 注释): @覆盖 @PreAuthorize(“允许全部”) @PostAuthorize("permitAll") 公共可迭代 findAll...

回答 2 投票 0

如何对 jpa 存储库方法进行单元测试?

我已经编写了一个 JPA 存储库方法,现在我意识到无法进行单元测试。 任何人都可以建议如何对以下方法进行单元测试或如何重构我的存储库以便我...

回答 2 投票 0

IDEA项目配置问题

这发生在我的 IDEA 社区。 如图所示,我从intellij-idea官网下载了插件,然后从本地添加到我的IDEA中,然后就报错了。 我是java的新学习者。 哈...

回答 1 投票 0

使用新的 Spring UriComponentsBuilder 进行 URL 编码

我正在尝试使用spring的UriComponentsBuilder来生成一些用于oauth交互的url。查询参数包括回调 url 和参数值等实体,其中包含空格...

回答 6 投票 0

Spring EL - if else 条件

我正在尝试形成如下所示的 Spring 表达式 我正在尝试形成如下所示的 Spring 表达式 <bean id="merchantPayment2-cronTrigger" class="uk.co.xxx.batch.merchantpayment2.CronTrigger"> <property name="jobDetail" ref="merchantPayment2-jobDetail" /> <property name="cronExpression" value="#{ ${merchantPaymntRptJob.useDB}== true ? (applicationConfigurationService.merchantPaymentCronExpression) : ${merchantPaymntRptJob.cronExpression} }"/> </bean> 我的意思是,如果属性merchantPaymntRptJob.useDB为true,则使用服务从数据库获取值或使用属性文件中的值。 问题在于使用 applicationConfigurationService.merchantPaymentCronExpression 我尝试了不同的组合 - 添加 $ 、 # 或只是 () 但不起作用。你能帮忙吗? 感谢您的帮助。下面的一个现在正在工作。 #{ (${merchantPaymntRptJob.useDB}== true) ?(@applicationConfigurationService).merchantPaymentCronExpression :'${merchantPaymntRptJob.cronExpression}'}

回答 1 投票 0

迭代方法中的通量内容,直到找到非空列表。 Spring WebFLux

我很难提出这个问题,因为我没有找到与我类似的用例,我刚刚开始使用WebFLux。 我有一个 getIdEntrevista 方法,它返回一个 Flux,其中包含...

回答 1 投票 0

Spring Cloud 流与 RabbitMQ 发送任何日期,如 LocalDateTime 或 Instant

每当存在具有任何日期(如 LocalDateTime、Instant 等)的对象时,我都会遇到从 Rabbit 队列消费消息的问题。 这是我的设置: Spring引导应用程序 @SpringBootApplicat...

回答 1 投票 0

SQL 会话在夜间流量较少时开始失败

我的团队面临一个问题,即我们的 SQL 会话在夜间流量较少时无法插入/更新/删除。 我们的代码在白天完美运行。晚上发生的事情是我们能够得到一个

回答 1 投票 0

Spring - 反应式代码 - 更改不传播

我正在尝试了解反应式流的传播及其运作方式 所以用例可以说我有一个预订列表,我按年龄分组并按...

回答 1 投票 0

尝试运行Spring boot 3.2.4应用程序时获取org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class

我正在开发 Spring Boot 3.2.4 应用程序,我已将密文放置在 application.properties 文件中并解密它,我在 pom.xml 文件中添加了下面提到的依赖项,但是当我运行时...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.