在服务中注入SecurityContext

时间:2017-06-22 07:32:44

标签: java security jax-rs javax

我的应用程序设置如下:

资源

@Path("/books")
public class BookResource {
    @Inject
    BookService bookService;

    @Context
    SecurityContext securityContext;

    @GET
    public Response getBooks() {
        List<BookDTO> books = bookService.getAllBooks();

        return Response.ok(books).build();
    }
}

服务

public interface BookService {
    List<BookDTO> getAllBooks();
}

ServiceImpl

public class BookServiceImpl implements BookService {

    @Context
    SecurityContext securityContext;

    @Override
    public List<BookDTO> getAllBooks() {
        BookDTO book1 = new BookDTO("Catcher in the Rye");
        BookDTO book2 = new BookDTO("Moby Dick");

        return Arrays.asList(new Book[]{book1,book2});
    }
}

在我的资源中,注入了SecurityContext,我可以获取当前用户。

有没有办法在Resource(我放置路径注释的地方)之外注入SecurityContext?如果是这样,我该怎么做? 我想将我的安全性移回服务,也可能是存储库。

更新 我通过以下代码解决了它,但我认为它可以变得更好/更清洁。

BaseResource

public class BaseResource {

    @Context
    SecurityContext securityContext;


    public class BaseRequest {

        private Principal principal;

        public BaseRequest() {
            principal = securityContext.getUserPrincipal();
        }

        public Principal getPrincipal() {
            return principal;
        }
    }
}

BookResource

public class BookResource extends BaseResource {

    @Inject
    BookService bookService;

    @Path("/{id}")
    public Response getBookById(@PathParam("id") Long id) {
        BookDTO book = bookService.getBookById(new GetBookRequest(id));

        return Response.ok(book).build();
    }

    public Response getAllBooks() {
        List<BookDTO > books = bookService.getAllBooks(new GetAllBooksRequest());

        return Response.ok(books).build();
    }

    public class GetBookRequest extends BaseRequest {

        private Long id;

        public GetBookRequest(Long id) {
            super();
            this.id = id;
        }

        public Long getId() {
            return id;
        }
    }

    public class GetAllBooksRequest extends BaseRequest {

        public GetAllBooksRequest() {
            super();
        }
    }
}

bookService的

public interface BookService {

    public List<BookDTO> getAllBooks(GetAllBooksRequest request);

    public BookDTO getBookById(GetBookRequest request);
}

BookServiceImpl

@Named
public class BookServiceImpl implements BookService {

    @Override
    public List<BookDTO> getAllBooks(GetAllBooksRequest request) {
        Principal principal = request.getPrincipal();

        BookDTO book1 = new BookDTO();
        book1.setName("Catcher in the Rye");
        book1.setId(1L);

        BookDTO book2 = new BookDTO();
        book2.setName("Moby Dick");
        book2.setId(2L);

        return Arrays.asList( new BookDTO[]{ book1, book2 });
    }

    @Override
    public BookDTO getBookById(GetBookRequest request) {
        Principal principal = request.getPrincipal();

        BookDTO book = new BookDTO();
        book.setName("Catcher in the Rye");
        book.setId(request.getId());

        return book;
    }

}

0 个答案:

没有答案