org.apache.tomcat.util.descriptor.web.SecurityConstraint Java Examples

The following examples show how to use org.apache.tomcat.util.descriptor.web.SecurityConstraint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpsConfiguration.java    From nbp with Apache License 2.0 7 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(redirectConnector());
    return tomcat;
}
 
Example #2
Source File: TomcatHttpConfig.java    From Java-API-Test-Examples with Apache License 2.0 7 votes vote down vote up
/**
 * 配置内置的Servlet容器工厂为Tomcat
 * @return
 */
@Bean
public ServletWebServerFactory servletContainer() {
	TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
		@Override
		protected void postProcessContext(Context context) {
			SecurityConstraint securityConstraint = new SecurityConstraint();
			securityConstraint.setUserConstraint("CONFIDENTIAL");
			SecurityCollection collection = new SecurityCollection();
			collection.addPattern("/*");
			securityConstraint.addCollection(collection);
			context.addConstraint(securityConstraint);
		}
	};
	//添加配置信息,主要是Http的配置信息
	tomcat.addAdditionalTomcatConnectors(redirectConnector());
	return tomcat;
}
 
Example #3
Source File: HttpsServerConfig.java    From micro-service with MIT License 6 votes vote down vote up
private void addSecurityConstraint(Context context) {
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setUserConstraint("CONFIDENTIAL");
    SecurityCollection collection = new SecurityCollection();
    collection.addPattern("/*");
    collection.addMethod("HEAD");
    collection.addMethod("PUT");
    collection.addMethod("DELETE");
    collection.addMethod("OPTIONS");
    collection.addMethod("TRACE");
    collection.addMethod("COPY");
    collection.addMethod("SEARCH");
    collection.addMethod("PROPFIND");
    securityConstraint.addCollection(collection);
    context.addConstraint(securityConstraint);
}
 
Example #4
Source File: KeycloakBaseSpringBootConfiguration.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private List<io.undertow.servlet.api.SecurityConstraint> getSecurityConstraints() {

            List<io.undertow.servlet.api.SecurityConstraint> undertowSecurityConstraints = new ArrayList<io.undertow.servlet.api.SecurityConstraint>();
            for (KeycloakSpringBootProperties.SecurityConstraint constraintDefinition : keycloakProperties.getSecurityConstraints()) {

                io.undertow.servlet.api.SecurityConstraint undertowSecurityConstraint = new io.undertow.servlet.api.SecurityConstraint();
                undertowSecurityConstraint.addRolesAllowed(constraintDefinition.getAuthRoles());

                for (KeycloakSpringBootProperties.SecurityCollection collectionDefinition : constraintDefinition.getSecurityCollections()) {

                    WebResourceCollection webResourceCollection = new WebResourceCollection();
                    webResourceCollection.addHttpMethods(collectionDefinition.getMethods());
                    webResourceCollection.addHttpMethodOmissions(collectionDefinition.getOmittedMethods());
                    webResourceCollection.addUrlPatterns(collectionDefinition.getPatterns());

                    undertowSecurityConstraint.addWebResourceCollections(webResourceCollection);

                }

                undertowSecurityConstraints.add(undertowSecurityConstraint);
            }
            return undertowSecurityConstraints;
        }
 
Example #5
Source File: TestSSOnonLoginAndDigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void setUpDigest(Tomcat tomcat) throws Exception {

        // Must have a real docBase for webapps - just use temp
        Context ctxt = tomcat.addContext(CONTEXT_PATH_DIGEST,
                System.getProperty("java.io.tmpdir"));
        ctxt.setSessionTimeout(SHORT_TIMEOUT_SECS);

        // Add protected servlet
        Tomcat.addServlet(ctxt, "TesterServlet3", new TesterServlet());
        ctxt.addServletMappingDecoded(URI_PROTECTED, "TesterServlet3");
        SecurityCollection collection = new SecurityCollection();
        collection.addPatternDecoded(URI_PROTECTED);
        SecurityConstraint sc = new SecurityConstraint();
        sc.addAuthRole(ROLE);
        sc.addCollection(collection);
        ctxt.addConstraint(sc);

        // Configure the appropriate authenticator
        LoginConfig lc = new LoginConfig();
        lc.setAuthMethod("DIGEST");
        ctxt.setLoginConfig(lc);
        ctxt.getPipeline().addValve(new DigestAuthenticator());
    }
 
Example #6
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints04() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // * is any app role
    // User role is not in any constraint
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    userRoles.add(ROLE99);
    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_ROLES);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, false);
}
 
Example #7
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints05() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // * is any app role
    // User role is a non-app constraint role
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    userRoles.add(ROLE1);
    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_ROLES);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, true);
}
 
Example #8
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints06() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // * is any app role
    // User role is an app role
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    userRoles.add(ROLE2);
    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_ROLES);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, true);
}
 
Example #9
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints08() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // ** is any authenticated user
    // User has no role
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_AUTHENTICATED_USERS);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, true);
}
 
Example #10
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints09() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // ** is any authenticated user
    // User has constraint role
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    userRoles.add(ROLE1);
    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_AUTHENTICATED_USERS);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, true);
}
 
Example #11
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints10() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // ** is any authenticated user
    // User has app role
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    userRoles.add(ROLE2);
    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_AUTHENTICATED_USERS);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, true);
}
 
Example #12
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints11() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // ** is any authenticated user
    // User is not authenticated
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_AUTHENTICATED_USERS);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(null, constraintOneRoles, constraintTwoRoles,
            applicationRoles, false);
}
 
Example #13
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints16() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // Constraint with empty auth section prevents all access
    // User has matching role
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    userRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_ROLES);
    applicationRoles.add(ROLE1);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, false);
}
 
Example #14
Source File: CorsConfig.java    From DrivingAgency with MIT License 6 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
    TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint=new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection=new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(connector);
    return tomcat;
}
 
Example #15
Source File: CustomConfig.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Bean
TomcatServletWebServerFactory tomcatServletWebServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint constraint = new SecurityConstraint();
            constraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            constraint.addCollection(collection);
            context.addConstraint(constraint);
        }
    };
    factory.addAdditionalTomcatConnectors(createTomcatConnector());
    return factory;
}
 
Example #16
Source File: SSLConfig.java    From NoteBlog with MIT License 6 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected void postProcessContext(Context context) {
            if (environment.getProperty("server.ssl.enabled", Boolean.class, Boolean.FALSE)) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            } else {
                super.postProcessContext(context);
            }
        }
    };
    if (environment.getProperty("server.ssl.enabled", Boolean.class, Boolean.FALSE)) {
        tomcat.addAdditionalTomcatConnectors(httpConnector());
    }
    return tomcat;
}
 
Example #17
Source File: WebConfig.java    From jcart with MIT License 6 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory servletContainer() {
	TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
		@Override
		protected void postProcessContext(Context context) {
			SecurityConstraint securityConstraint = new SecurityConstraint();
			securityConstraint.setUserConstraint("CONFIDENTIAL");
			SecurityCollection collection = new SecurityCollection();
			collection.addPattern("/*");
			securityConstraint.addCollection(collection);
			context.addConstraint(securityConstraint);
		}
	};

	tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
	return tomcat;
}
 
Example #18
Source File: HttpsConfig.java    From spring-boot-demo with MIT License 6 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(connector);
    return tomcat;
}
 
Example #19
Source File: MaxKeySslConfig.java    From MaxKey with Apache License 2.0 6 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(connector);
    return tomcat;
}
 
Example #20
Source File: SslConfig.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            // SecurityConstraint必须存在,可以通过其为不同的URL设置不同的重定向策略。
            SecurityConstraint constraint = new SecurityConstraint();
            constraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            constraint.addCollection(collection);
            context.addConstraint(constraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(httpConnector());
    return tomcat;
}
 
Example #21
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints17() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // Constraint with empty auth section prevents all access
    // User matches all authenticated users
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    userRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_AUTHENTICATED_USERS);
    applicationRoles.add(ROLE1);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, false);
}
 
Example #22
Source File: SystemConfiguration.java    From NFVO with Apache License 2.0 6 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory servletContainer() {
  if (https) {
    TomcatEmbeddedServletContainerFactory tomcat =
        new TomcatEmbeddedServletContainerFactory() {
          @Override
          protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
          }
        };

    tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
    return tomcat;
  }
  return new TomcatEmbeddedServletContainerFactory();
}
 
Example #23
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCombineConstraints07() throws IOException {
    // Allowed roles should be the union of the roles in the constraints
    // * is any app role
    // User has no role
    List<String> userRoles = new ArrayList<>();
    List<String> constraintOneRoles = new ArrayList<>();
    List<String> constraintTwoRoles = new ArrayList<>();
    List<String> applicationRoles = new ArrayList<>();

    constraintOneRoles.add(ROLE1);
    constraintTwoRoles.add(SecurityConstraint.ROLE_ALL_ROLES);
    applicationRoles.add(ROLE2);
    applicationRoles.add(ROLE3);

    doRoleTest(userRoles, constraintOneRoles, constraintTwoRoles,
            applicationRoles, false);
}
 
Example #24
Source File: Http2Https.java    From springBoot with MIT License 5 votes vote down vote up
@Bean
public TomcatServletWebServerFactory servletContainerFactory() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            //设置安全性约束
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            //设置约束条件
            SecurityCollection collection = new SecurityCollection();
            //拦截所有请求
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    //设置将分配给通过此连接器接收到的请求的方案
    connector.setScheme("http");

    //true: http使用http, https使用https;
    //false: http重定向到https;
    connector.setSecure(false);

    //设置监听请求的端口号,这个端口不能其他已经在使用的端口重复,否则会报错
    connector.setPort(httpPort);

    //重定向端口号(非SSL到SSL)
    connector.setRedirectPort(sslPort);

    tomcat.addAdditionalTomcatConnectors(connector);
    return tomcat;
}
 
Example #25
Source File: TestWebSocketFrameClient.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testConnectToBasicEndpoint() throws Exception {

    Tomcat tomcat = getTomcatInstance();
    Context ctx = tomcat.addContext(URI_PROTECTED, null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");

    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded("/");
    String utf8User = "test";
    String utf8Pass = "123\u00A3"; // pound sign

    tomcat.addUser(utf8User, utf8Pass);
    tomcat.addRole(utf8User, ROLE);

    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctx.addConstraint(sc);

    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    ctx.setLoginConfig(lc);

    AuthenticatorBase basicAuthenticator = new org.apache.catalina.authenticator.BasicAuthenticator();
    ctx.getPipeline().addValve(basicAuthenticator);

    tomcat.start();

    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_USER_NAME, utf8User);
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PASSWORD, utf8Pass);

    echoTester(URI_PROTECTED, clientEndpointConfig);

}
 
Example #26
Source File: TestSSOnonLoginAndDigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void setUpNonLogin(Tomcat tomcat) throws Exception {

        // Must have a real docBase for webapps - just use temp
        Context ctxt = tomcat.addContext(CONTEXT_PATH_NOLOGIN,
                System.getProperty("java.io.tmpdir"));
        ctxt.setSessionTimeout(LONG_TIMEOUT_SECS);

        // Add protected servlet
        Tomcat.addServlet(ctxt, "TesterServlet1", new TesterServlet());
        ctxt.addServletMappingDecoded(URI_PROTECTED, "TesterServlet1");
        SecurityCollection collection1 = new SecurityCollection();
        collection1.addPatternDecoded(URI_PROTECTED);
        SecurityConstraint sc1 = new SecurityConstraint();
        sc1.addAuthRole(ROLE);
        sc1.addCollection(collection1);
        ctxt.addConstraint(sc1);

        // Add unprotected servlet
        Tomcat.addServlet(ctxt, "TesterServlet2", new TesterServlet());
        ctxt.addServletMappingDecoded(URI_PUBLIC, "TesterServlet2");
        SecurityCollection collection2 = new SecurityCollection();
        collection2.addPatternDecoded(URI_PUBLIC);
        SecurityConstraint sc2 = new SecurityConstraint();
        // do not add a role - which signals access permitted without one
        sc2.addCollection(collection2);
        ctxt.addConstraint(sc2);

        // Configure the appropriate authenticator
        LoginConfig lc = new LoginConfig();
        lc.setAuthMethod("NONE");
        ctxt.setLoginConfig(lc);
        ctxt.getPipeline().addValve(new NonLoginAuthenticator());
    }
 
Example #27
Source File: TestRestCsrfPreventionFilter2.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void setUpApplication() throws Exception {
    context = tomcat.addContext(CONTEXT_PATH_LOGIN, System.getProperty("java.io.tmpdir"));
    context.setSessionTimeout(SHORT_SESSION_TIMEOUT_MINS);

    Tomcat.addServlet(context, SERVLET_NAME, new TesterServlet());
    context.addServletMappingDecoded(URI_PROTECTED, SERVLET_NAME);

    FilterDef filterDef = new FilterDef();
    filterDef.setFilterName(FILTER_NAME);
    filterDef.setFilterClass(RestCsrfPreventionFilter.class.getCanonicalName());
    filterDef.addInitParameter(FILTER_INIT_PARAM, REMOVE_CUSTOMER + "," + ADD_CUSTOMER);
    context.addFilterDef(filterDef);

    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(FILTER_NAME);
    filterMap.addURLPatternDecoded(URI_CSRF_PROTECTED);
    context.addFilterMap(filterMap);

    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded(URI_PROTECTED);

    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    context.addConstraint(sc);

    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod(METHOD);
    context.setLoginConfig(lc);

    AuthenticatorBase basicAuthenticator = new BasicAuthenticator();
    context.getPipeline().addValve(basicAuthenticator);
}
 
Example #28
Source File: TestWebSocketFrameClient.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testConnectToDigestEndpoint() throws Exception {

    Tomcat tomcat = getTomcatInstance();
    Context ctx = tomcat.addContext(URI_PROTECTED, null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");

    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded("/*");

    tomcat.addUser(USER, PWD);
    tomcat.addRole(USER, ROLE);

    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctx.addConstraint(sc);

    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("DIGEST");
    ctx.setLoginConfig(lc);

    AuthenticatorBase digestAuthenticator = new org.apache.catalina.authenticator.DigestAuthenticator();
    ctx.getPipeline().addValve(digestAuthenticator);

    tomcat.start();

    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_USER_NAME, USER);
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PASSWORD,PWD);

    echoTester(URI_PROTECTED, clientEndpointConfig);

}
 
Example #29
Source File: RealmBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Convert an ArrayList to a SecurityConstraint [].
 */
private SecurityConstraint [] resultsToArray(
        ArrayList<SecurityConstraint> results) {
    if(results == null || results.size() == 0) {
        return null;
    }
    SecurityConstraint [] array = new SecurityConstraint[results.size()];
    results.toArray(array);
    return array;
}
 
Example #30
Source File: TestDigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    // Configure a context with digest auth and a single protected resource
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctxt = tomcat.addContext(CONTEXT_PATH, null);

    // Add protected servlet
    Tomcat.addServlet(ctxt, "TesterServlet", new TesterServlet());
    ctxt.addServletMappingDecoded(URI, "TesterServlet");
    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded(URI);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctxt.addConstraint(sc);

    // Configure the Realm
    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser(USER, PWD);
    realm.addUserRole(USER, ROLE);
    ctxt.setRealm(realm);

    // Configure the authenticator
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("DIGEST");
    lc.setRealmName(REALM);
    ctxt.setLoginConfig(lc);
    ctxt.getPipeline().addValve(new DigestAuthenticator());
}