未验证 提交 4f33170c 编写于 作者: C CharlieXCL 提交者: GitHub

support micro-profile-sureness examples (#137)

* support micro-profile-sureness examples

* Update samples/micro-profile-sureness/README.md
上级 1b93b4bd
# MicroProfile generated Application
## Introduction
MicroProfile Starter has generated this MicroProfile application for you.
The generation of the executable jar file can be performed by issuing the following command
mvn clean package
mvn clean install -Pwildfly
This will create an executable jar file **demo-wildfly.jar** within the _target_ maven folder. This can be started by executing the following command
java -jar target/demo-wildfly.jar
To launch the test page, open your browser at the following URL
http://localhost:8080/index.html
## Specification examples
By default, there is always the creation of a JAX-RS application class to define the path on which the JAX-RS endpoints are available.
Also, a simple Hello world endpoint is created, have a look at the class **HelloController**.
More information on MicroProfile can be found [here](https://microprofile.io/)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>samples</artifactId>
<groupId>com.usthe.sureness</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>micro-profile-sureness</artifactId>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.wildfly>23.0.0.Final</version.wildfly>
<failOnMissingWebXml>false</failOnMissingWebXml>
<final.name>demo</final.name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.install.skip>true</maven.install.skip>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>4.0.1</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.usthe.sureness</groupId>
<artifactId>sureness-core</artifactId>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
</build>
<profiles>
<profile>
<id>wildfly</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-jar-maven-plugin</artifactId>
<version>1.0.0.Alpha4</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<feature-pack-location>wildfly@maven(org.jboss.universe:community-universe)#${version.wildfly}</feature-pack-location>
<layers>
<layer>jaxrs</layer>
</layers>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
<version>1.0.1.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jboss-public-repository</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>Red Hat GA</id>
<name>Red Hat GA</name>
<url>https://maven.repository.redhat.com/ga/</url>
</repository>
</repositories>
</profile>
</profiles>
</project>
\ No newline at end of file
package com.usthe.sureness.microprofile;
import com.usthe.sureness.DefaultSurenessConfig;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
* @author Lenovo
*/
@ApplicationPath("/")
public class DemoRestApplication extends Application {
public DemoRestApplication(){
super();
new DefaultSurenessConfig(DefaultSurenessConfig.SUPPORT_JAX_RS);
}
}
package com.usthe.sureness.microprofile;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
/**
* @author Lenovo
*/
@Path("/hello")
@Singleton
public class HelloController {
@GET
public String sayHello() {
return "Hello World";
}
}
package com.usthe.sureness.microprofile;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
/**
* @author Lenovo
*/
@Path("api")
public class ResourceController {
/** access success message **/
public static final String SUCCESS_ACCESS_RESOURCE = "access this resource: %s success";
@GET
@Path("v1/bar/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String api1Mock1(@Context Request servletRequest, @PathParam("id") String id) {
return String.format(SUCCESS_ACCESS_RESOURCE, "get /api/v1/bar/" + id);
}
@POST
@Path("v1/bar")
@Produces(MediaType.TEXT_PLAIN)
public String api1Mock2(@Context Request servletRequest) {
return String.format(SUCCESS_ACCESS_RESOURCE, "post /api/v1/bar");
}
@PUT
@Path("v2/bar")
@Produces(MediaType.TEXT_PLAIN)
public String api1Mock3(@Context Request servletRequest) {
return String.format(SUCCESS_ACCESS_RESOURCE, "put /api/v2/bar");
}
@GET
@Path("v2/foo")
@Produces(MediaType.TEXT_PLAIN)
public String api1Mock4(@Context Request servletRequest) {
return String.format(SUCCESS_ACCESS_RESOURCE, "get /api/v2/foo");
}
@DELETE
@Path("v2/foo")
@Produces(MediaType.TEXT_PLAIN)
public String api1Mock5(@Context Request servletRequest) {
return String.format(SUCCESS_ACCESS_RESOURCE, "delete /api/v2/foo");
}
@GET
@Path("v3/foo")
@Produces(MediaType.TEXT_PLAIN)
public String api1Mock6(@Context Request servletRequest) {
return String.format(SUCCESS_ACCESS_RESOURCE, "get /api/v3/foo");
}
}
package com.usthe.sureness.microprofile;
import com.usthe.sureness.mgt.SurenessSecurityManager;
import com.usthe.sureness.processor.exception.*;
import com.usthe.sureness.subject.SubjectSum;
import com.usthe.sureness.util.SurenessContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.container.*;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
/**
* @author Lenovo
*/
@Provider
@PreMatching
public class SurenessFilter implements ContainerRequestFilter, ContainerResponseFilter {
/** logger **/
private static final Logger logger = LoggerFactory.getLogger(SurenessFilter.class);
@Override
public void filter(ContainerRequestContext requestContext) {
try {
SubjectSum subject = SurenessSecurityManager.getInstance().checkIn(requestContext);
// You can consider using SurenessContextHolder to bind subject in threadLocal
// if bind, please remove it when end
if (subject != null) {
SurenessContextHolder.bindSubject(subject);
}
} catch (IncorrectCredentialsException | UnknownAccountException | ExpiredCredentialsException e1) {
logger.debug("this request account info is illegal");
requestContext.abortWith(Response.status(401).entity(e1.getMessage()).build());
} catch (NeedDigestInfoException e4) {
logger.debug("you should try once again with digest auth information");
requestContext.abortWith(Response.status(401).header("WWW-Authenticate", e4.getAuthenticate()).build());
} catch (UnauthorizedException e5) {
logger.debug("this account can not access this resource");
requestContext.abortWith(Response.status(403).entity(e5.getMessage()).build());
} catch (RuntimeException e) {
logger.error("other exception happen: ", e);
requestContext.abortWith(Response.status(500).entity(e.getMessage()).build());
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
SurenessContextHolder.clear();
}
}
## -- sureness.yml document dataSource-- ##
# load api resource which need be protected, config role who can access these resource.
# resources that are not configured are also authenticated and protected by default, but not authorized
# eg: /api/v1/bar===post===[role1] means /api/v1/bar===post can be access by role1
# eg: /api/v3/foo===get===[] means /api/v3/foo===* can not be access by any role
resourceRole:
- /api/v1/bar/*===get===[role1,role2,role3]
- /api/v1/bar===post===[role1]
- /api/v2/bar===put===[role2]
- /api/v2/foo===get===[role3]
- /api/v3/foo===get===[]
# load api resource which do not need be protected, means them need be excluded.
# these api resource can be access by everyone
# eg: /**/*.png===* means get/post/put/delete/patch any url suffixed with `.png` can be access by everyone
excludedResource:
- /api/v2/foo===delete
- /**/*.html===get
- /**/*.js===get
- /**/*.css===get
- /**/*.ico===*
- /**/*.png===*
- /hello
# account info
# there are three account: sam, tom, lisa
# eg: sam has [role1,role2,role3], password is sam123, has salt -> 123
# eg: tom has role2, password is tom123
# eg: lisa has role3, password is lisa123
account:
- appId: sam
# if add salt, the credential is encrypted by md5 - result is: MD5(password+salt)
# digest auth not support encrypted credential
credential: 1B9E9136628CB1B161AE47132DD7AF19
salt: 123
role: [role1,role2,role3]
- appId: tom
credential: tom123
role: [role2]
- appId: lisa
credential: lisa123
role: [role3]
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
## -- sureness.yml document dataSource-- ##
# load api resource which need be protected, config role who can access these resource.
# resources that are not configured are also authenticated and protected by default, but not authorized
# eg: /api/v1/bar===post===[role1] means /api/v1/bar===post can be access by role1
# eg: /api/v3/foo===get===[] means /api/v3/foo===* can not be access by any role
resourceRole:
- /api/v1/bar/*===get===[role1,role2,role3]
- /api/v1/bar===post===[role1]
- /api/v2/bar===put===[role2]
- /api/v2/foo===get===[role3]
- /api/v3/foo===get===[]
# load api resource which do not need be protected, means them need be excluded.
# these api resource can be access by everyone
# eg: /**/*.png===* means get/post/put/delete/patch any url suffixed with `.png` can be access by everyone
excludedResource:
- /api/v2/foo===delete
- /**/*.html===get
- /**/*.js===get
- /**/*.css===get
- /**/*.ico===*
- /**/*.png===*
- /hello
# account info
# there are three account: sam, tom, lisa
# eg: sam has [role1,role2,role3], password is sam123, has salt -> 123
# eg: tom has role2, password is tom123
# eg: lisa has role3, password is lisa123
account:
- appId: sam
# if add salt, the credential is encrypted by md5 - result is: MD5(password+salt)
# digest auth not support encrypted credential
credential: 1B9E9136628CB1B161AE47132DD7AF19
salt: 123
role: [role1,role2,role3]
- appId: tom
credential: tom123
role: [role2]
- appId: lisa
credential: lisa123
role: [role3]
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Eclipse MicroProfile demo</title>
</head>
<body>
<h2>MicroProfile</h2>
<a href="#" target="_blank" >Hello JAX-RS endpoint</a> <br/>
</body>
</html>
......@@ -26,6 +26,7 @@
<module>spring-gateway-sureness</module>
<module>zuul-sureness</module>
<module>sureness-spring-boot-starter-example</module>
<module>micro-profile-sureness</module>
</modules>
<dependencyManagement>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册