我失去了很多工作,因为我没有完成任务.当我尝试完成我的2天工作时,我发现了它.在rebase没有解决之前我无法提交作品,所以我中止它,因为跳过或继续不起作用.但是随着中止我所有的改变都消失了......你能告诉我是否有可能恢复这些数据吗?谢谢你的建议.
在我们的微服务中,我们将拥有自定义身份验证/授权服务,又名 UAA,对吗?使用用户控制器来登录、令牌验证或创建用户对我来说很有意义。
但还有第二个用例。我们还希望有一些用户管理功能,用于向用户添加收藏夹联系人、使用一些附加信息、用户地址等填充个人资料。在第二个用例中,我希望为此目的提供第二个微服务。您有什么建议或最佳实践是什么?
有一个用于用户管理的微服务,例如他的个人资料、联系人、凭据以及登录/令牌提供
有两个微服务 - 一个用于管理用户凭据、令牌提供(uaa),第二个用于用户附加信息
有两个微服务 - 一个用于完全独立的用户管理,第二个用于身份验证、令牌提供,并且如果需要一些用户数据或验证用户凭据,则将使用用户服务的其余 api
对我来说,3.选项很好,但我想听听你的意见。
architecture authentication spring authorization microservices
你能告诉我是否存在某种漂亮的方法将一个实体对象映射到另一个 dto 对象?当我需要转换为时List<ObjEntity>,List<ObjDTO>我创建了该函数:
public class Converter {
public static Function<ObjEntity, ObjDTO> MYOBJ_ENTITY_TO_DTO = objEntity -> {
ObjDTO dto = new ObjDTO();
dto.setId(objEntity.getId());
dto.setName(objEntity.getName());
dto.setNote(objEntity.getNote());
// ...
return dto;
};
}
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
List<ObjDTO> dtos = objEntitiesList.stream().map(Converter.MYOBJ_ENTITY_TO_DTO).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但是如果我只需要转换一个对象怎么办?我应该使用该功能MYOBJ_ENTITY_TO_DTO以及如何使用该功能?或者什么是最佳实践?是的,我可以在 Converter 类中执行经典函数,如下所示:
public static ObjEntity dtoToEntity(ObjDTO dto) {
// map here entity to dto and return entity
}
Run Code Online (Sandbox Code Playgroud)
但这是旧风格。java 8中存在一些新的做法吗?类似于我的 lambda 列表示例?
我需要测试我的控制器方法,包括删除方法.这是部分控制器代码:
@RestController
@RequestMapping("/api/foo")
public class FooController {
@Autowired
private FooService fooService;
// other methods which works fine in tests
@RequestMapping(path="/{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable Long id) {
fooService.delete(id);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
@InjectMocks
private FooController fooController;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(fooController)
.setControllerAdvice(new ExceptionHandler()).alwaysExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).build();
}
@Test
public void testFooDelete() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders
.delete("/api/foo")
.param("id", "11")
.contentType(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
Run Code Online (Sandbox Code Playgroud)
因错误的状态代码而导致测试失败:
java.lang.AssertionError:预期状态:200实际:400
在控制台日志中我也发现了这个:
2017-12-11 20:11:01 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - DispatcherServlet with name '' processing DELETE request for …Run Code Online (Sandbox Code Playgroud) 你能帮我解决我的问题吗?我使用 Spring OAuth2 为我的客户端生成了 JWT。我已经实现了一个授权和资源服务器以及一些网络安全配置,一切都通过在线指南完成。
它工作正常,但现在我想编辑有效负载并添加自定义属性,例如用户的名字和姓氏等。你能检查我的代码并告诉我我应该怎么做才能将其他属性添加到有效负载中吗?谢谢。
这是我的实现:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${security.signing-key}")
private String signingKey;
@Value("${security.encoding-strength}")
private Integer encodingStrength;
@Value("${security.security-realm}")
private String securityRealm;
@Autowired
private UserDetailsService userDetailsService;
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.httpBasic()
.realmName(securityRealm)
.and()
.csrf()
.disable();
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = …Run Code Online (Sandbox Code Playgroud) 在我的Maven项目中,我具有以下结构:
docker/
docker-compose.yml
A/
Dockerfile
B/
Dockerfile
src/
target/
foo.war
Run Code Online (Sandbox Code Playgroud)
在A的Dockerfile中,我需要使用/target以下命令访问文件夹中的war :
COPY ../../target/foo.war /usr/local/tomcat/webapps/foo.war
Run Code Online (Sandbox Code Playgroud)
当我运行docker-compose uptt给我错误
生成失败:COPY失败:生成上下文之外的禁止路径:../../target/foo.war
docker-compose.yml
version: '3.6'
services:
fooA:
build: ./docker/A
ports:
- "8080:8080"
depends_on:
- fooB
fooB:
build: ./docker/fooB
ports:
- "5433:5433"
Run Code Online (Sandbox Code Playgroud)
你能告诉我如何解决吗?我不想在每个项目构建后手动复制war文件。
我需要在基于 Spring Boot 的微服务应用程序中解决原子操作。您可以想象 2 个微服务,如图所示。
一项服务用于保存用户权限和凭据作为身份提供者,另一项服务作为应用程序中用于与订单、联系人、地址等配对的用户帐户。
当用户创建新帐户时,我需要创建用于商业目的的用户和用于授权的用户身份。这两个操作都应该作为一个原子操作来执行。这意味着如果失败,机器人必须失败并回滚。如果没有业务用户对象,则无法保存用户凭据。您能告诉我解决此问题的最佳方法是什么或应该采取哪些步骤吗?
我的第一个想法是使用 Feign 的 RestTemplate 调用另一个服务,但我不知道如果第二次失败如何回滚操作。谢谢你的建议。
spring distributed-transactions spring-boot microservices spring-cloud
我有这样的实体:
@Getter
@Setter
@Entity
public class Conversation extends AbstractEntity{
@ElementCollection
@Column(name = "user_id", nullable = false)
@CollectionTable(name = "conversation_user", joinColumns = @JoinColumn(name = "conversation_id", nullable = false))
private List<String> usersIds;
}
Run Code Online (Sandbox Code Playgroud)
是否可以通过用户 id 的精确匹配通过 spring 的存储库找到对话实体?例如我有这些实体:
id | user_ids
------------------------------------------
1 | user-a, user-b, user-c
2 | user-a, user-b
3 | user-a, user-c
Run Code Online (Sandbox Code Playgroud)
因此,当我需要通过用户 IDuser-a和user-c常规 IN 子句找到对话时,如下所示:
SELECT c FROM Conversation c WHERE c.userIds IN :userIds
会找到 id 1 和 3 的对话,但我想找到完全匹配的,所以我的预期结果只是对话 3。
可能的解决方案是在存储库中使用常规 IN 子句,在服务层中使用下一个过滤器集合,但我更喜欢直接从数据库返回所需实体的解决方案。至少可以在 …
我的应用程序通常工作正常,但是当我运行测试或通过 maven 构建应用程序时,应用程序关闭了由于错误 java.lang.NullPointerException 的应有测试。我调试了它并发现我的服务层中的 bean 没有自动装配并且它们为空。这是我的测试课:
public class CompanyServiceSimpleTest {
private CompanyService companyService;
@Before
public void setUp() {
companyService = new CompanyServiceImpl();
}
// Here is sample test
@Test
public void testNumberOfCompanies() {
Assert.assertEquals(2, companyService.findAll().size());
}
}
Run Code Online (Sandbox Code Playgroud)
companyService 已初始化,但其中的 bean 未初始化。这是 CompanyServiceImpl:
@Service
public class CompanyServiceImpl implements CompanyService {
@Autowired
private CompanyRepository companyRepository; // is null
@Autowired
private NotificationService notificationService; // is null
@Override
public List<CompanyDto> findAll() {
List<CompanyEntity> entities = companyRepository.find(0, Integer.MAX_VALUE);
return entities.stream().map(Translations.COMPANY_DOMAIN_TO_DTO).collect(Collectors.toList());
}
// ... some …Run Code Online (Sandbox Code Playgroud) 我已经配置了资源服务器,用于根据身份验证服务器验证 JWT 令牌。在下面的代码中,您可以看到我已定义的配置(是来自Auth0issuer-uri的 URI )。如果用户在我的公共客户端上针对 Auth0 进行身份验证,则该客户端会从 Auth0 接收 JWT 令牌。当我使用令牌标头调用资源服务器时,用户已获得授权,并且资源可用,但仅包含从 JWT 解析的基本数据,而不包含有关用户的完整信息。我有来自 Auth0 的可用端点,它提供用户名、图片、电子邮件等。SecurityContextHolderuserinfo
我的问题是,我是否可以在资源服务器中设置此用户信息端点,以自动获取此信息,或者最好的方法是什么?我希望将此信息包含在SecurityContextHolder或至少包含用户的电子邮件和用户名中。
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
http.authorizeRequests().anyRequest().permitAll()
.and()
.oauth2ResourceServer().jwt();
return http.build()
}
Run Code Online (Sandbox Code Playgroud)
和 JWT 解码器 bean
@Bean
fun jwtDecoder(): JwtDecoder? {
val jwtDecoder = JwtDecoders.fromOidcIssuerLocation<JwtDecoder>(issuer) as NimbusJwtDecoder
val audienceValidator: OAuth2TokenValidator<Jwt> = AudienceValidator(audience)
val withIssuer = JwtValidators.createDefaultWithIssuer(issuer)
val withAudience: OAuth2TokenValidator<Jwt> = DelegatingOAuth2TokenValidator(withIssuer, audienceValidator)
jwtDecoder.setJwtValidator(withAudience)
return jwtDecoder
}
Run Code Online (Sandbox Code Playgroud)
文件application.properties
spring.security.oauth2.resourceserver.jwt.issuer-uri=my-domain.com
spring.security.oauth2.resourceserver.jwt.audience=my-audience
Run Code Online (Sandbox Code Playgroud)
编辑这是从 Auth0 收到的 JWT 的有效负载
{
"iss": …Run Code Online (Sandbox Code Playgroud)