|
From: Mark V. <mar...@uz...> - 2015-04-16 15:33:51
|
Hi
I would like to enable basic authentication and @RolesAllowed for restEasy on UndertowJaxrsServer
Can someone help me out how to enable this
I found some references to set resteasy.role.based.security but I did not find a way how to set this in my DeploymentInfo
<context-param>
<param-name>resteasy.role.based.security</param-name>
<param-value>true</param-value>
</context-param>
Some of my code I have been trying is. But it is not working.
webServer = new UndertowJaxrsServer();
Undertow.Builder serverBuilder=Undertow.builder();
serverBuilder = serverBuilder.addHttpListener(Integer.parseInt(properties.getProperty("port")), properties.getProperty("address"));
webServer.start(serverBuilder);
HashMap users = new HashMap(2);
users.put("userOne", "passwordOne".toCharArray());
users.put("userTwo", "passwordTwo".toCharArray());
MapIdentityManager identityManager = new MapIdentityManager(users);
DeploymentInfo di = webServer.undertowDeployment(MyApp.class) ;
di.setClassLoader(GetRest.class.getClassLoader()) ;
di.setDeploymentName("My Application");
di.setContextPath("/di");
LoginConfig loginConfig=new LoginConfig("BASIC","MyRealm");
di.setLoginConfig(loginConfig);
di.setIdentityManager(identityManager);
webServer.deploy(di);
webServer.deploy(MyApp.class);
class MapIdentityManager implements IdentityManager {
private final Map<String, char[]> users;
public MapIdentityManager(Map<String, char[]> users) {
this.users = users;
}
@Override
public Account verify(Account account) {
return account;
}
@Override
public Account verify(String id, Credential credential) {
Account account = this.getAccount(id);
return account != null && this.verifyCredential(account, credential)?account:null;
}
@Override
public Account verify(Credential credential) {
return null;
}
private boolean verifyCredential(Account account, Credential credential) {
if(credential instanceof PasswordCredential) {
char[] password = ((PasswordCredential)credential).getPassword();
char[] expectedPassword = (char[])this.users.get(account.getPrincipal().getName());
return Arrays.equals(password, expectedPassword);
} else {
return false;
}
}
private Account getAccount(final String id) {
return this.users.containsKey(id)?new Account() {
private final Principal principal = new Principal() {
@Override
public String getName() {
return id;
}
};
@Override
public Principal getPrincipal() {
return this.principal;
}
@Override
public Set<String> getRoles() {
return Collections.emptySet();
}
}:null;
}
}
|