简体   繁体   English

Eclipse Scout-干净的数据库身份验证

[英]Eclipse Scout - Clean Database authentication

I'm trying to implement a database authentication with Eclipse Scout. 我正在尝试使用Eclipse Scout实现数据库身份验证。

For that I created a class DataSourceCredentialVerifier in the client module, which implements the ICredentialVerifier interface. 为此,我在客户端模块中创建了一个DataSourceCredentialVerifier类,该类实现了ICredentialVerifier接口。 Then I adapted the init method of the UiServletFilter class to use my verifier. 然后,我修改了UiServletFilter类的init方法以使用我的验证程序。

public class DataSourceCredentialVerifier implements ICredentialVerifier {

private static final Logger LOG = LoggerFactory.getLogger(DataSourceCredentialVerifier.class);

@Override
public int verify(String username, char[] password) throws IOException {
    Object queryResult[][] = BEANS.get(IMySqlAuthService.class).load();


    return AUTH_OK;
}

I haven't implemented any authentication logic yet. 我尚未实现任何身份验证逻辑。 My task now is to establish a clean database connection. 我现在的任务是建立一个干净的数据库连接。

For that I created the following interface in the shared module: 为此,我在共享模块中创建了以下interface

public interface IMySqlAuthService extends IService {

    Object[][] load();
}

The implementation is in the server module: 实现在服务器模块中:

   public class MySqlAuthService implements IMySqlAuthService {

        @Override
        public Object[][] load() {
            String sql = "select username, password from users ";
            Object[][] queryResult = SQL.select(sql, null, null);       
            return queryResult;
        }
   }

First I want to see, if there is at least something in the query, but I get an AssertionException here: 首先,我想看看查询中是否至少有什么东西,但是我在这里得到一个AssertionException:

 Object queryResult[][] = BEANS.get(IMySqlAuthService.class).load();

org.eclipse.scout.rt.platform.util.Assertions$AssertionException: Assertion error: no instance found for query: interface org.eclipse.scout.app.shared.services.IMySqlAuthService
at org.eclipse.scout.rt.platform.util.Assertions.fail(Assertions.java:580)
at org.eclipse.scout.rt.platform.util.Assertions.assertNotNull(Assertions.java:87)
at org.eclipse.scout.rt.platform.BEANS.get(BEANS.java:41)

I don't get an instance of my MySqlAuthService implementation. 我没有MySqlAuthService实现的实例。 I assume that the BeanManager should have created an instance for me. 我假设BeanManager应该已经为我创建了一个实例。 MySqlAuthService should be registered as a Bean, since my IMySqlAuthService interface extends from IService which has the @ApplicationScoped annotation. MySqlAuthService应该注册为Bean,因为我的IMySqlAuthService接口是从具有@ApplicationScoped批注的IService扩展的。

Adding the @Bean annotation to MySqlAuthService results in the same exception. @Bean批注添加到MySqlAuthService导致相同的异常。

Here some information about the BeanManager and annotations: https://eclipsescout.github.io/6.0/technical-guide.html#sec-bean.manager 这里是有关BeanManager和注释的一些信息: https : BeanManager

Here is another different approach so tried, but it doesn't feel correct: https://www.eclipse.org/forums/index.php/t/1079741/ 这是尝试过的另一种不同方法,但感觉不正确: https//www.eclipse.org/forums/index.php/t/1079741/

How can I get my example to work with my service? 我怎样才能使我的榜样与我的服务一起工作?

Here is the working solution with important explanations of Eclipse Scout principles. 这是带有Eclipse Scout原理的重要说明的工作解决方案。

The source is summarized information of the Eclipse-Scout-Technical-Guide. 来源是Eclipse-Scout-Technical-Guide的摘要信息。

In Scout there is a built in annotation: @TunnelToServer . 在Scout中,有一个内置注释: @TunnelToServer Interfaces marked with this annotation are called on the server. 标有此批注的接口在服务器上被调用。 The server itself ignores this annotation. 服务器本身将忽略此注释。 To achieve that a bean is registered on client side, this annotation is required. 为了实现在客户端注册Bean,此注释是必需的。 The platform cannot (!) directly create an instance for these beans, a specific producer is registered which creates a proxy that delegates the call to the server. 平台无法 (!)直接为这些bean创建实例,注册了特定的生产者,该生产者创建了将调用委派给服务器的代理。

My first clear mistake was that I hadn't annotated the IMySqlAuthService with @TunnelToServer . 我的第一个明显的错误是我没有使用IMySqlAuthService注释@TunnelToServer

After this addition I got rid of the no instance AssertionError . 添加完之后,我摆脱了no instance AssertionError

After that my code ran into the HTTP status-code: 403 access forbidden. 之后,我的代码遇到了HTTP状态代码:禁止403访问。

This occured because my code didn't run in the correct Thread. 发生这种情况是因为我的代码未在正确的线程中运行。 That is the current RunContext . 那就是当前的RunContext I had to use this lines of code in my verify method of the DataSourceCredentialVerifier : 我必须在DataSourceCredentialVerifier verify方法中使用以下代码行:

     Subject subject = new Subject();
     subject.getPrincipals().add(new SimplePrincipal("system"));
     subject.setReadOnly();
     RunContext runContext = RunContexts.copyCurrent().withSubject(subject);

Now one can use the runContext's call() or run() method, depending whether the code returns a result. 现在,可以使用runContext的call()run()方法,具体取决于代码是否返回结果。 The action is run in the current thread, meaning that the caller is blocked until completion. 该操作在当前线程中运行,这意味着调用者将被阻止直到完成。

Concrete example solution: 具体示例解决方案:

Object[][] result = runContext.call(new Callable<Object[][]>() {

   @Override
   public Object[][] call() throws Exception {
      return BEANS.get(IMySqlAuthService.class).load();
   }

});

//TODO implement authentication logic.

For more information about the RunContext see here: https://eclipsescout.github.io/6.0/technical-guide.html#runcontext 有关RunContext更多信息,请参见此处: https : RunContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM