Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
feuyeux committed Apr 5, 2024
1 parent 39c2003 commit 5288ecd
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.persistence.criteria.CriteriaQuery;

import com.example.domain.Book;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
Expand All @@ -22,7 +21,6 @@
*/
@Repository
public class BookDao {
private static final Logger LOGGER = Logger.getLogger(BookDao.class);

@PersistenceContext
private EntityManager entityManager;
Expand All @@ -44,7 +42,7 @@ public Book findById(final Integer id) {
try {
return entityManager.find(Book.class, id);
} catch (final Exception e) {
BookDao.LOGGER.error(e);
//BookDao.LOGGER.error(e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

import javax.security.auth.login.LoginException;

import org.apache.log4j.Logger;

public class RestLoginDao {
public static final String USER_QUERY = "select user_name from users where user_name=? and user_pass=?";
public static final String ROLE_QUERY = "select role_name from user_roles where user_name=?";
private static final Logger LOG = Logger.getLogger(RestLoginDao.class);

public boolean isValidUser(final String userName, final char[] passWord) throws LoginException {
try (Connection connection = getConnection(); PreparedStatement stmt = connection.prepareStatement(
Expand All @@ -27,7 +24,6 @@ public boolean isValidUser(final String userName, final char[] passWord) throws
}
}
} catch (final Exception e) {
RestLoginDao.LOG.error("Error when loading user from the database " + e);
e.printStackTrace();
}
return false;
Expand All @@ -45,7 +41,6 @@ public List<String> getRoles(final String userName, final RestUserPrincipal user
}
}
} catch (final Exception e) {
RestLoginDao.LOG.error("Error when loading user from the database " + e);
e.printStackTrace();
}
return roleList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

import org.apache.log4j.Logger;

/*
* set JAVA_OPTS=
* -Djava.security.auth.login.config=D:\+aries\github\jax-rs2-guide\sample\6\security-rest\src\main\resources
* \restJaas.conf
*/
public class RestLoginModule implements LoginModule {
private static final Logger LOG = Logger.getLogger(RestLoginModule.class);

private Subject subject;
private CallbackHandler callbackHandler;
Expand Down Expand Up @@ -62,8 +60,6 @@ public boolean login() throws LoginException {
if (userName == null || passWord == null) {
throw new LoginException("Callback handler does not return login data properly");
}
RestLoginModule.LOG.info("username=" + userName);
//logger.info("password" + password);
if (dao.isValidUser(userName, passWord)) {
succeeded = true;
return succeeded;
Expand All @@ -76,7 +72,6 @@ public boolean login() throws LoginException {

@Override
public boolean commit() throws LoginException {
RestLoginModule.LOG.info("committing...");
if (!succeeded) {
return false;
} else {
Expand All @@ -100,10 +95,10 @@ public boolean commit() throws LoginException {
}
}
commitSucceeded = true;
RestLoginModule.LOG.info("principals=" + subject.getPrincipals());
//RestLoginModule.LOG.info("principals=" + subject.getPrincipals());
for (final Principal p : subject.getPrincipals()) {
if (p instanceof RestRolePrincipal) {
RestLoginModule.LOG.info(" ROLE: " + p.getName());
//RestLoginModule.LOG.info(" ROLE: " + p.getName());
}
}
return commitSucceeded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.example.domain.Book;
import com.example.domain.Books;
import com.example.service.BookService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
Expand All @@ -30,7 +29,6 @@
@Path("books")
@javax.annotation.security.RunAs("user")
public class BookResource {
private static final Logger LOGGER = Logger.getLogger(BookResource.class);
@Autowired
private BookService bookService;

Expand All @@ -45,17 +43,16 @@ public class BookResource {
public Books getBooks(@Context final SecurityContext sc) {
logMe(sc);
final Books books = bookService.getBooks();
BookResource.LOGGER.debug(books);
return books;
}

private void logMe(final SecurityContext sc) {
try {
BookResource.LOGGER.info("User=" + sc.getUserPrincipal().getName());
BookResource.LOGGER.info("User Role?=" + sc.isUserInRole("user"));
BookResource.LOGGER.info("Auth way=" + sc.getAuthenticationScheme());
// BookResource.LOGGER.info("User=" + sc.getUserPrincipal().getName());
// BookResource.LOGGER.info("User Role?=" + sc.isUserInRole("user"));
// BookResource.LOGGER.info("Auth way=" + sc.getAuthenticationScheme());
} catch (final Exception e) {
LOGGER.debug("Cannot print credential info." + e);
//LOGGER.debug("Cannot print credential info." + e);
}
}

Expand All @@ -70,7 +67,6 @@ private void logMe(final SecurityContext sc) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Book getBookByPath(@PathParam("bookId") final Integer bookId) {
final Book book = bookService.getBook(bookId);
BookResource.LOGGER.debug(book);
return book;
}

Expand All @@ -85,7 +81,6 @@ public Book getBookByPath(@PathParam("bookId") final Integer bookId) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Book getBookByQuery(@QueryParam("id") final Integer bookId) {
final Book book = bookService.getBook(bookId);
BookResource.LOGGER.debug(book);
return book;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.example.dao.BookDao;
import com.example.domain.Book;
import com.example.domain.Books;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -15,7 +14,6 @@
* @version $Id: $Id
*/
public class BookService {
private static final Logger LOGGER = Logger.getLogger(BookService.class);
@Autowired
private BookDao bookDao;

Expand Down Expand Up @@ -45,7 +43,6 @@ public Book getBook(final Integer bookId) {
try {
return bookDao.findById(bookId);
} catch (final Exception e) {
BookService.LOGGER.error(e);
return new Book(-1, "");
}
}
Expand Down
15 changes: 2 additions & 13 deletions 10.5.oauth2-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,15 @@
<module>client</module>
</modules>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring.security.version>5.8.11</spring.security.version>
<spring-boot.version>3.2.3</spring-boot.version>
<spring-security-oauth2.version>2.5.2.RELEASE</spring-security-oauth2.version>
<spring.version>5.3.32</spring.version>
<junit.version>4.13.1</junit.version>
<jersey.version>2.27</jersey.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<source>${JDK.version}</source>
<target>${JDK.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
Expand Down
13 changes: 0 additions & 13 deletions 2.3.6-1.simple-service-moxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

<properties>
<JDK.version>1.8</JDK.version>
<guava.version>32.0.0-jre</guava.version>
<jersey.version>2.35</jersey.version>
<jquery.version>3.6.0</jquery.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
<maven-eclipse-plugin.version>2.9</maven-eclipse-plugin.version>
<junit.version>4.13.1</junit.version>
<slf4j.version>1.7.32</slf4j.version>
<log4j.version>1.2.15</log4j.version>
</properties>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

import com.example.domain.Book;
import com.example.domain.Books;
import org.apache.log4j.Logger;


@Path("books")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class BookResource {
private static final Logger LOGGER = Logger.getLogger(BookResource.class);
private static final HashMap<Long, Book> memoryBase;

static {
Expand All @@ -40,27 +39,23 @@ public Books getBooks() {
final List<Book> bookList = new ArrayList<>();
final Set<Map.Entry<Long, Book>> entries = BookResource.memoryBase.entrySet();
for (Entry<Long, Book> cursor : entries) {
BookResource.LOGGER.debug(cursor.getKey());
bookList.add(cursor.getValue());
}
final Books books = new Books(bookList);
BookResource.LOGGER.debug(books);
return books;
}

@Path("{bookId:[0-9]*}")
@GET
public Book getBookByPath(@PathParam("bookId") final Long bookId) {
final Book book = BookResource.memoryBase.get(bookId);
BookResource.LOGGER.debug(book);
return book;
}

@Path("/book")
@GET
public Book getBookByQuery(@QueryParam("id") final Long bookId) {
final Book book = BookResource.memoryBase.get(bookId);
BookResource.LOGGER.debug(book);
return book;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@

import com.example.domain.Book;
import com.example.domain.Books;
import org.apache.log4j.Logger;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;

public class JsonTest extends JerseyTest {
private final static Logger LOGGER = Logger.getLogger(JsonTest.class);

@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
Expand All @@ -25,7 +22,7 @@ protected Application configure() {
public void testGettingBooks() {
Books books = target("books").request(MediaType.APPLICATION_JSON_TYPE).get(Books.class);
for (Book book : books.getBookList()) {
LOGGER.debug(book.getBookName());
//LOGGER.debug(book.getBookName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

@Path("books")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class BookResource {
private static final Logger LOGGER = Logger.getLogger(BookResource.class);
private static final HashMap<Long, JsonObject> memoryBase;

static {
Expand Down Expand Up @@ -54,7 +51,7 @@ public JsonArray getBooks() {
for (Entry<Long, JsonObject> cursor : entries) {
Long key = cursor.getKey();
JsonObject value = cursor.getValue();
BookResource.LOGGER.debug(key);
//BookResource.LOGGER.debug(key);
arrayBuilder.add(value);
}
return arrayBuilder.build();
Expand All @@ -64,7 +61,7 @@ public JsonArray getBooks() {
@GET
public JsonObject getBookByQuery(@QueryParam("id") final Long bookId) {
final JsonObject book = BookResource.memoryBase.get(bookId);
BookResource.LOGGER.debug(book);
//
return book;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;

public class JsonTest extends JerseyTest {
private final static Logger LOGGER = Logger.getLogger(JsonTest.class);

@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
Expand All @@ -30,22 +27,19 @@ public void testSaveBook() {
JsonObject newBook = Json.createObjectBuilder().add("bookName", "Java EE 7 精髓").add("publisher", "人邮").build();
Entity<JsonObject> entity = Entity.entity(newBook, MediaType.APPLICATION_JSON_TYPE);
JsonObject book = target("books").request(MediaType.APPLICATION_JSON_TYPE).post(entity, JsonObject.class);
LOGGER.debug(book.getJsonNumber("bookId") + "\t" + book.getString("bookName"));
}

@Test
public void testGetBook() {
WebTarget booksTarget = target("books").path("book").queryParam("id", 2);
JsonObject book = booksTarget.request(MediaType.APPLICATION_JSON_TYPE).get(JsonObject.class);
LOGGER.debug(book.getJsonNumber("bookId") + "\t" + book.getString("bookName"));
}

@Test
public void testGetBooks() {
JsonArray books = target("books").request(MediaType.APPLICATION_JSON_TYPE).get(JsonArray.class);
for (JsonValue jsonValue : books) {
JsonObject book = (JsonObject)jsonValue;
LOGGER.debug(book.getJsonNumber("bookId") + "\t" + book.getString("bookName"));
}
}
}
Loading

0 comments on commit 5288ecd

Please sign in to comment.