Best Citrus code snippet using com.consol.citrus.http.security.User
Source:EndpointConfig.java
...23import com.consol.citrus.http.server.HttpServer;24import com.consol.citrus.message.Message;25import com.consol.citrus.xml.namespace.NamespaceContextBuilder;26import org.apache.http.auth.AuthScope;27import org.apache.http.auth.UsernamePasswordCredentials;28import org.eclipse.jetty.security.*;29import org.eclipse.jetty.util.security.Credential;30import org.springframework.context.annotation.Bean;31import org.springframework.context.annotation.Configuration;32import org.springframework.http.HttpStatus;33import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;34import javax.security.auth.Subject;35import java.io.IOException;36import java.security.Principal;37import java.util.Collections;38import java.util.List;39/**40 * @author Christoph Deppisch41 */42@Configuration43public class EndpointConfig {44 private static final String[] USER_ROLES = new String[] { "CitrusRole" };45 private final int port = 8090;46 @Bean47 public NamespaceContextBuilder namespaceContextBuilder() {48 NamespaceContextBuilder namespaceContextBuilder = new NamespaceContextBuilder();49 namespaceContextBuilder.setNamespaceMappings(Collections.singletonMap("xh", "http://www.w3.org/1999/xhtml"));50 return namespaceContextBuilder;51 }52 @Bean53 public HttpClient todoClient() {54 return CitrusEndpoints55 .http()56 .client()57 .requestUrl("http://localhost:" + port)58 .build();59 }60 @Bean61 public HttpClient todoBasicAuthClient() throws Exception {62 return CitrusEndpoints63 .http()64 .client()65 .requestUrl("http://localhost:" + port)66 .requestFactory(basicAuthRequestFactory())67 .build();68 }69 @Bean70 public BasicAuthClientHttpRequestFactory basicAuthRequestFactoryBean() {71 BasicAuthClientHttpRequestFactory requestFactory = new BasicAuthClientHttpRequestFactory();72 AuthScope authScope = new AuthScope("localhost", port, "", "basic");73 requestFactory.setAuthScope(authScope);74 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("citrus", "secr3t");75 requestFactory.setCredentials(credentials);76 return requestFactory;77 }78 @Bean79 public HttpComponentsClientHttpRequestFactory basicAuthRequestFactory() throws Exception {80 return basicAuthRequestFactoryBean().getObject();81 }82 @Bean83 public HttpServer basicAuthHttpServer() throws Exception {84 return CitrusEndpoints85 .http()86 .server()87 .port(port)88 .endpointAdapter(staticEndpointAdapter())89 .securityHandler(basicAuthSecurityHandler())90 .autoStart(true)91 .build();92 }93 @Bean94 public StaticEndpointAdapter staticEndpointAdapter() {95 return new StaticEndpointAdapter() {96 @Override97 protected Message handleMessageInternal(Message message) {98 return new HttpMessage("<todo xmlns=\"http://citrusframework.org/samples/todolist\">" +99 "<id>100</id>" +100 "<title>todoName</title>" +101 "<description>todoDescription</description>" +102 "</todo>")103 .status(HttpStatus.OK);104 }105 };106 }107 @Bean108 public SecurityHandlerFactory basicAuthSecurityHandlerFactoryBean() {109 SecurityHandlerFactory securityHandlerFactory = new SecurityHandlerFactory();110 securityHandlerFactory.setUsers(users());111 securityHandlerFactory.setLoginService(basicAuthLoginService(basicAuthUserStore()));112 securityHandlerFactory.setConstraints(Collections.singletonMap("/todo/*", new BasicAuthConstraint(USER_ROLES)));113 return securityHandlerFactory;114 }115 @Bean116 public SecurityHandler basicAuthSecurityHandler() throws Exception {117 return basicAuthSecurityHandlerFactoryBean().getObject();118 }119 @Bean120 public HashLoginService basicAuthLoginService(PropertyUserStore basicAuthUserStore) {121 return new HashLoginService() {122 @Override123 protected void doStart() throws Exception {124 setUserStore(basicAuthUserStore);125 basicAuthUserStore.start();126 super.doStart();127 }128 };129 }130 @Bean131 public PropertyUserStore basicAuthUserStore() {132 return new PropertyUserStore() {133 @Override134 protected void loadUsers() throws IOException {135 getKnownUserIdentities().clear();136 for (User user : users()) {137 Credential credential = Credential.getCredential(user.getPassword());138 Principal userPrincipal = new AbstractLoginService.UserPrincipal(user.getName(),credential);139 Subject subject = new Subject();140 subject.getPrincipals().add(userPrincipal);141 subject.getPrivateCredentials().add(credential);142 String[] roleArray = IdentityService.NO_ROLES;143 if (user.getRoles() != null && user.getRoles().length > 0) {144 roleArray = user.getRoles();145 }146 for (String role : roleArray) {147 subject.getPrincipals().add(new AbstractLoginService.RolePrincipal(role));148 }149 subject.setReadOnly();150 getKnownUserIdentities().put(user.getName(), getIdentityService().newUserIdentity(subject, userPrincipal, roleArray));151 }152 }153 };154 }155 private List<User> users() {156 return Collections.singletonList(new User("citrus", "secr3t", USER_ROLES));157 }158}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!