小编Eth*_*son的帖子

Spring Data Rest Pageable Child Collection

我有一个名为User的@Entity.它有一组变更集如下:

@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user")
private Set<Changeset> changesets = new HashSet<Changeset>();
Run Code Online (Sandbox Code Playgroud)

我有一个UserRepository:

@Repository
@RestResource(path = "users", rel = "users")
public interface UserRepository extends JpaRepository<User, Long>{ }
Run Code Online (Sandbox Code Playgroud)

还有一个ChangesetRepository:

@Repository
@RestResource(path = "changesets", rel = "changesets")
public interface ChangesetRepository extends JpaRepository<Changeset, Long> { }
Run Code Online (Sandbox Code Playgroud)

调用GET http://localhost:8080/changesets/http://localhost:8080/users/产生分页响应.

如果我打开GET,http://localhost:8080/users/1/changesets那么我将所有结果都放在一个数组中,并且不会发生分页.

有没有办法向Spring Data Rest表明我想通过其父用户访问它时以可分页的方式返回changesets集合?变更集集将快速增长,我宁愿不在单个页面中返回大量结果.

编辑:

正如Willie Wheeler所建议的那样,我将其添加到我的ChangesetRepository中以使其可搜索:

@RestResource(path = "byUser", rel = "byUser")
public Page<Changeset> findByUser(@Param("id") User user, Pageable p);
Run Code Online (Sandbox Code Playgroud)

我将关系保留为双向,但也可以通过使用更改集来隐藏用户更改集的链接@RestResource(exported=false).

旁注:似乎将关系设置为exported = false会隐藏链接,但实际上不会删除映射./ users/1/changesets未公布,但仍然有效.

spring hibernate spring-data spring-data-jpa spring-data-rest

12
推荐指数
1
解决办法
2957
查看次数

Spring Data Rest Ambiguous Association Exception

新添加的LinkCollectingAssociationHandlerMappingException由于我的域类中的模糊关联而引发的.

links数组如下所示:

[<http://localhost:8080/rooms/2/roomGroups>;rel="roomGroups", <http://localhost:8080/rooms/2/userGroup>;rel="userGroup", <http://localhost:8080/rooms/2/room>;rel="room", <http://localhost:8080/rooms/2/originatingConferences>;rel="originatingConferences", <http://localhost:8080/rooms/2/user>;rel="user"]

它正在尝试在抛出异常时添加另一个"房间"关系.

问题是它似乎在添加我已明确标记的关系链接 @RestResource(exported = false)

以下是我认为导致此问题的关系示例:

@JsonIgnore
@RestResource(exported = false)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.room", cascade = {CascadeType.REMOVE})
private Set<RoomsByUserAccessView> usersThatCanDisplay = new HashSet<>(); 
Run Code Online (Sandbox Code Playgroud)

该类型RoomsByUserAccessView具有由a Room和a 组成的嵌入式id User.

我还注释了嵌入式id属性:

@JsonIgnore
@RestResource(exported = false)
private RoomsByUserAccessViewId pk = new RoomsByUserAccessViewId();
Run Code Online (Sandbox Code Playgroud)

和它的属性如下:

@JsonIgnore
@RestResource(exported = false)
private Room room;

@JsonIgnore
@RestResource(exported = false)
private User userWithAccess;

public RoomsByUserAccessViewId() {
    //
}
Run Code Online (Sandbox Code Playgroud)

在序列化为JSON时,如何才能正确忽略这些关系?

我的代码在DATAREST-262之前工作(https://github.com/spring-projects/spring-data-rest/commit/1d53e84cae3d09b09c4b5a9a4caf438701527550)

我尝试访问房间/端点时返回的完整错误消息如下: …

spring json spring-mvc spring-data-rest spring-hateoas

9
推荐指数
1
解决办法
2450
查看次数

@NamedQuery覆盖Spring Data Rest JpaRepository中的findAll

有没有办法覆盖Spring Data Rest执行的findAll查询?

我需要一种基于某些特定标准过滤结果的方法,似乎使用a @NamedQuery应该是我正在寻找的线,所以我设置了一个测试.

@Entity
@Table(name = "users")
@NamedQueries({
    @NamedQuery(name = "User.findAll", query="SELECT u FROM User u WHERE u.username = 'test'"), 
    @NamedQuery(name = "User.findNameEqualsTest", query="SELECT u FROM User u WHERE u.username = 'test'")   
})
public class User implements Serializable, Identifiable<Long> { }
Run Code Online (Sandbox Code Playgroud)

有了这个,我希望SDR能够利用我的findAll()查询(返回1个结果),而是执行相同的旧findAll逻辑(返回所有结果).

在我的存储库中,我添加了:

@Repository
@RestResource(path = "users", rel = "users")
public interface UserJpaRepository extends JpaRepository<User, Long> {

    public Page<User> findNameEqualsTest(Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它会拿起提供的@NamedQuery.所以...

我该如何重写默认findAll()逻辑?我需要实际构建一组复杂的标准并将其应用于结果集.

spring jpa spring-data spring-data-jpa spring-data-rest

8
推荐指数
2
解决办法
2万
查看次数

@Autowired in Spring PermissionEvaluator

首先,我已经广泛搜索了这一点,虽然看起来有一个固定的位置我无法成功引用注入的@Bean内部PermissionEvaluator:

https://jira.springsource.org/browse/SEC-2136?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

在该问题的评论部分,Rob Winch提供了一个解决方案的建议

要解决此问题,您可以使用LazyInitTargetSource代理您的permissionEvaluator

话虽这么说,我在实现发布的XML的基于注释的JavaConfig版本时遇到了麻烦.我正在使用Spring Boot 1.0.0.BUILD-SNAPSHOT和spring-boot-starter-security.

我有一个类来配置方法安全性,如下所示:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {                   

    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {

        DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
        expressionHandler.setPermissionEvaluator(new MyPermissionEvaluator());
        expressionHandler.setParameterNameDiscoverer(new SimpleParameterDiscoverer());

        return expressionHandler;
    }
}
Run Code Online (Sandbox Code Playgroud)

并开始PermissionEvaluator:

public class MyPermissionEvaluator implements PermissionEvaluator {

    private static final Logger LOG = LoggerFactory.getLogger(MyPermissionEvaluator.class); 

    @Autowired
    private UserRepository userRepo;    

    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {     

    if (authentication == null || !authentication.isAuthenticated()) {
        return …
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-aop spring-boot

6
推荐指数
2
解决办法
5387
查看次数

Spring OAuth2在表单登录时不会重定向回客户端

我正在使用OAuth2开发示例Spring Boot应用程序.问题是客户端托管在localhost:8080调用https://localhost:8443/oauth/authorize自己(隐式授权类型),但由于/oauth/authorize要求用户进行身份验证,他们将被重定向到登录页面https://localhost:8443/login.

这是所有预期的,但是当用户登陆登录页面时,所有查询字符串(包括redirect_uri)都将丢失.用户登录并重定向到https://localhost:8443而不是指定的redirect_uri http://localhost:8080.

在使用服务器的登录表单登录后,是否有某种方法可以将用户重定向回客户端?我在配置中遗漏了什么吗?我可以根据需要发布更多内容.

授权请求如下: https://localhost:8443/oauth/authorize?response_type=token&state=6c2bb162-0f26-4caa-abbe-b65f7e5c6a2e&redirect_uri=http%3A%2F%2Flocalhost%3A8080&client_id=admin

SecurityConfig:

@Configuration
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final Logger log = LoggerFactory.getLogger(WebSecurityConfig.class);                        

    @Override
    public void configure(WebSecurity web) throws Exception {

        web.ignoring().antMatchers("/resources/**");
    }       

    @SuppressWarnings("deprecation")
    @Override
    protected void configure(HttpSecurity http) throws Exception {                  

        http
            .requestMatchers()
                .antMatchers("/**")
        .and()
            .addFilterAfter(new CsrfCookieGeneratorFilter(), CsrfFilter.class)
            .exceptionHandling()
                .accessDeniedPage("/login?authorization_error=true")
        .and()
            .authorizeRequests()
            .antMatchers("/resources/**", "/csrf").permitAll()
            .anyRequest().authenticated()
        .and()
            .formLogin()
                .loginPage("/login")
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .defaultSuccessUrl("/", false)
                .failureUrl("/login?authentication_error=true")
                .permitAll()
        .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login") …
Run Code Online (Sandbox Code Playgroud)

spring oauth spring-security spring-boot spring-security-oauth2

6
推荐指数
1
解决办法
4628
查看次数

@PreAuthorize对JpaRepository

我希望为我的REST服务实现基于角色的安全性.我正在使用spring-data-rest并已配置JpaRepository如下:

@Repository
@RestResource(path = "changesets", rel = "changesets")
public interface ChangesetRepository extends JpaRepository<Changeset, Long> { }
Run Code Online (Sandbox Code Playgroud)

我想@PreAuthorize在继承的Page<T> findAll(Pageable pageable)方法上附加一个注释,以便GET需要一个特定的角色.

有没有办法做到这一点?我需要提供自定义实现还是我遗漏了一些明显的东西?

spring spring-security spring-data-jpa spring-data-rest

3
推荐指数
2
解决办法
1555
查看次数