简体   繁体   English

dropwizard休眠使用服务器内部的资源

[英]dropwizard hibernate use a resource from within the server

I'm new to dropwizard and i'm trying to create an Authenticator which gets credentials from the user, then it uses the rest api getUser method which i implemented in my UserResouce class to get the user with the username that is in the credentials from the db users table. 我是dropwizard的新手,我正尝试创建一个从用户获取凭据的Authenticator,然后它使用在我的UserResouce类中实现的rest api getUser方法来获取具有凭据中用户名的用户db users表。 However in my autheticator class i having troubles in figuring out how to use the user resource functions to get the user. 但是,在我的身份验证器类中,我在弄清楚如何使用用户资源功能来吸引用户方面遇到了麻烦。

I was trying to do something like that: 我正在尝试做这样的事情:

public List<com.amitbaz.tss.db.User> getUsersFromDB(String username){

    SessionFactory sessionFactory = TradingSystemServerApplication.hibernateBundle.getSessionFactory();
    UserDAO userDAO = new UserDAO(sessionFactory);
    List<com.amitbaz.tss.db.User> user = userDAO.getUser(username);
    logger.debug(user.toString());
    return user;
}

inside the autheticator and call it from the authenticte function but it says there is no session bound... 在身份验证器中并从authenticte函数调用它,但它说没有会话绑定...

EDIT: 编辑:

Ok so after much thinking i got to this: I'm dropwizard authenticator and authorizer implementions with BasicCredentials and. 好了,经过深思熟虑,我明白了这一点:我是使用BasicCredentials和的dropwizard身份验证器和授权者实现。

Autheticator (don't mind the VALID_USER thing..): 验证者(不要介意VALID_USER的事情。):

package com.amitbaz.tss.auth;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.persistence.NamedQuery;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amitbaz.tss.TradingSystemServerApplication;
import com.amitbaz.tss.db.UserDAO;
import com.amitbaz.tss.db.UserResource;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator;
import io.dropwizard.auth.basic.BasicCredentials;

public class TradingSystemServerAuthenticator implements Authenticator<BasicCredentials, User> {

    private Logger logger = LoggerFactory.getLogger(TradingSystemServerAuthenticator.class);

    private static final Map<String, Set<String>> VALID_USERS = ImmutableMap.of(
            "guest", ImmutableSet.of(),
            "amit", ImmutableSet.of("admin"),
            "stav", ImmutableSet.of("broker")
        );

    private UserDAO userDAO;

    public TradingSystemServerAuthenticator(UserDAO userDAO) {
        // TODO Auto-generated constructor stub
        this.userDAO = userDAO;
    }

    @Override
    public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException {
        // TODO Auto-generated method stub
        List<com.amitbaz.tss.db.User> user = userDAO.getUser(credentials.getUsername());
        logger.debug(user.toString());
        if("amit".equals(credentials.getPassword())){
            return Optional.of(new User(credentials.getUsername(), VALID_USERS.get(credentials.getUsername())));
        }
        return Optional.empty();
    }

}

Authorizer: 授权人:

package com.amitbaz.tss.auth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amitbaz.tss.db.UserDAO;

import io.dropwizard.auth.Authorizer;

public class TradingSystemServerAuthorizer implements Authorizer<User>{

    private Logger logger = LoggerFactory.getLogger(TradingSystemServerAuthorizer.class);

    private UserDAO userDAO;



    public TradingSystemServerAuthorizer(UserDAO userDAO) {
        super();
        this.userDAO = userDAO;
    }



    @Override
    public boolean authorize(User user, String role) {
        // TODO Auto-generated method stub
        logger.debug(userDAO.getUser(user.getName()).toString());
        return user.getName().equals("amit") && user.getRole().contains(new String("admin"));
    }

}

Now, In my Application class I do this: 现在,在我的Application类中,我这样做:

package com.amitbaz.tss;


import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amitbaz.tss.auth.TradingSystemServerAuthenticator;
import com.amitbaz.tss.auth.TradingSystemServerAuthorizer;
import com.amitbaz.tss.auth.User;
import com.amitbaz.tss.db.Broker;
import com.amitbaz.tss.db.BrokerDAO;
import com.amitbaz.tss.db.BrokerResource;
import com.amitbaz.tss.db.Contact;
import com.amitbaz.tss.db.ContactDAO;
import com.amitbaz.tss.db.ContactResource;
import com.amitbaz.tss.db.Product;
import com.amitbaz.tss.db.ProductDAO;
import com.amitbaz.tss.db.ProductResource;
import com.amitbaz.tss.db.Test;
import com.amitbaz.tss.db.TestDAO;
import com.amitbaz.tss.db.TestResource;
import com.amitbaz.tss.db.Transaction;
import com.amitbaz.tss.db.TransactionDAO;
import com.amitbaz.tss.db.TransactionResource;
import com.amitbaz.tss.db.UserDAO;
import com.amitbaz.tss.db.UserResource;
import com.amitbaz.tss.db.UserRole;
import com.amitbaz.tss.db.UserRoleDAO;
import com.amitbaz.tss.db.UserRoleResource;
import com.amitbaz.tss.db.Website;
import com.amitbaz.tss.db.WebsiteDAO;
import com.amitbaz.tss.db.WebsiteResource;

import io.dropwizard.Application;
import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import javassist.tools.web.Webserver;

public class TradingSystemServerApplication extends Application<TradingSystemServerConfiguration>{

    public static void main(String[] args) throws Exception{
        new TradingSystemServerApplication().run(args);
    }

    public final static HibernateBundle<TradingSystemServerConfiguration> hibernateBundle
    = new HibernateBundle<TradingSystemServerConfiguration>(
            Test.class,Broker.class, com.amitbaz.tss.db.User.class, UserRole.class
            ,Product.class, Transaction.class, Website.class, Contact.class
    ) {
        @Override
        public DataSourceFactory getDataSourceFactory(
                TradingSystemServerConfiguration configuration
        ) {
            return configuration.getDataSourceFactory();
        }
    };

    final Logger logger = LoggerFactory.getLogger(TradingSystemServerApplication.class);

    @Override
    public void initialize(
            final Bootstrap<TradingSystemServerConfiguration> bootstrap) {
        bootstrap.addBundle(hibernateBundle);
    }

    @Override
    public void run(TradingSystemServerConfiguration config, Environment env) throws Exception {
        final UserDAO userDAO = new UserDAO(hibernateBundle.getSessionFactory());
        final UserRoleDAO userRoleDAO = new 
        env.jersey().register(new UserResource(userDAO));

       /...
BasicCredentialAuthFilter.Builder<User>()
                .setAuthenticator(new TradingSystemServerAuthenticator(userDAO))
                .setAuthorizer(new TradingSystemServerAuthorizer(userDAO))
                .setRealm("Authetication Required")
                .buildAuthFilter()));
        env.jersey().register(RolesAllowedDynamicFeature.class);
        env.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));

    }

}

And I have the annotation @RolesAllowed("role_name") on one of the rest api methods which with im trying to test the auth. 我在其余api方法之一上使用了@RolesAllowed(“ role_name”)批注,而我尝试测试身份验证。

Now when i try to test this and i make a request to that rest api method, I get the error No session currently bound to execution context where i do userDAO.getUser(...) in the authanticator and in the authorizer 现在,当我尝试对此进行测试并向该其余api方法发出请求时,出现错误No session currently bound to execution context ,其中在身份验证器和授权者中执行userDAO.getUser(...)

EDIT 2: 编辑2:

UserDAO implementation: UserDAO的实现:

package com.amitbaz.tss.db;

import java.util.List;

import org.hibernate.SessionFactory;

import io.dropwizard.hibernate.AbstractDAO;

public class UserDAO extends AbstractDAO<User>{

    public UserDAO(SessionFactory sessionFactory) {
        super(sessionFactory);
        // TODO Auto-generated constructor stub
    }

    public List<User> getUser(String username){
        return list(namedQuery("com.amitbaz.tss.db.user.getUser")
                .setParameter("username", username));
    }

}

EDIT 3: 编辑3:

Added @UnitOfWork to authenticate and authorize methods. 添加了@UnitOfWork以对方法进行身份验证和授权。

registered them as follow ( Notice the changes in hibernateBundle and run method): package com.amitbaz.tss; 按照如下方式注册它们(注意hibernateBundle和run方法中的更改):com.amitbaz.tss包;

import javax.servlet.ServletRegistration;

import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.cpr.AtmosphereServlet;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amitbaz.tss.auth.TradingSystemServerAuthenticator;
import com.amitbaz.tss.auth.TradingSystemServerAuthorizer;
import com.amitbaz.tss.auth.User;
import com.amitbaz.tss.db.Broker;
import com.amitbaz.tss.db.BrokerDAO;
import com.amitbaz.tss.db.BrokerResource;
import com.amitbaz.tss.db.Contact;
import com.amitbaz.tss.db.ContactDAO;
import com.amitbaz.tss.db.ContactResource;
import com.amitbaz.tss.db.Product;
import com.amitbaz.tss.db.ProductDAO;
import com.amitbaz.tss.db.ProductResource;
import com.amitbaz.tss.db.Test;
import com.amitbaz.tss.db.TestDAO;
import com.amitbaz.tss.db.TestResource;
import com.amitbaz.tss.db.Transaction;
import com.amitbaz.tss.db.TransactionDAO;
import com.amitbaz.tss.db.TransactionResource;
import com.amitbaz.tss.db.UserDAO;
import com.amitbaz.tss.db.UserResource;
import com.amitbaz.tss.db.UserRole;
import com.amitbaz.tss.db.UserRoleDAO;
import com.amitbaz.tss.db.UserRoleResource;
import com.amitbaz.tss.db.Website;
import com.amitbaz.tss.db.WebsiteDAO;
import com.amitbaz.tss.db.WebsiteResource;

import io.dropwizard.Application;
import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import javassist.tools.web.Webserver;

public class TradingSystemServerApplication extends Application<TradingSystemServerConfiguration>{

    public static void main(String[] args) throws Exception{
        new TradingSystemServerApplication().run(args);
    }

    public final static HibernateBundle<TradingSystemServerConfiguration> hibernateBundle
    = new HibernateBundle<TradingSystemServerConfiguration>(
            Test.class,Broker.class, com.amitbaz.tss.db.User.class, UserRole.class
            ,Product.class, Transaction.class, Website.class, Contact.class
            ,TradingSystemServerAuthenticator.class, TradingSystemServerAuthorizer.class
    ) {
        @Override
        public DataSourceFactory getDataSourceFactory(
                TradingSystemServerConfiguration configuration
        ) {
            return configuration.getDataSourceFactory();
        }
    };

    final Logger logger = LoggerFactory.getLogger(TradingSystemServerApplication.class);

    @Override
    public void initialize(
            final Bootstrap<TradingSystemServerConfiguration> bootstrap) {
        bootstrap.addBundle(hibernateBundle);
    }

    @Override
    public void run(TradingSystemServerConfiguration config, Environment env) throws Exception {
        final UserDAO userDAO = new UserDAO(hibernateBundle.getSessionFactory());
        final UserRoleDAO userRoleDAO = new UserRoleDAO(hibernateBundle.getSessionFactory());

        final TradingSystemServerAuthorizer authorizer = new TradingSystemServerAuthorizer(userDAO);
        final TradingSystemServerAuthenticator authenticator = new TradingSystemServerAuthenticator(userDAO);

        env.jersey().register(new UserResource(userDAO));
        env.jersey().register(new UserRoleResource(userRoleDAO));

        env.jersey().register(authorizer);
        env.jersey().register(authenticator);

        env.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<User>()
                .setAuthenticator(authenticator)
                .setAuthorizer(authorizer)
                .setRealm("Authetication Required")
                .buildAuthFilter()));
        env.jersey().register(RolesAllowedDynamicFeature.class);
        env.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));


}

Your approach looks like a design issue. 您的方法看起来像一个设计问题。 The issue I see is that you are trying to integrate via rest with a service that is already accessible for you within your application. 我看到的问题是,您正在尝试通过Rest与您的应用程序中已经可以访问的服务集成。 That adds a lot of overhead and complicates things. 这增加了很多开销并使事情复杂化。

Fortunately, DW already has a fully integrated Authorization and Authentication system just waiting for you to plug in. You can read more about it here: http://www.dropwizard.io/1.0.2/docs/manual/auth.html 幸运的是,DW已经有一个完全集成的授权和身份验证系统,正等着您插入。您可以在此处了解更多信息: http : //www.dropwizard.io/1.0.2/docs/manual/auth.html

The essential thing to note here is that you should split the service used by your resource from your resource. 这里要注意的基本事情是您应该将资源使用的服务与资源分开。 In your case for example the UserDao, or you could split it into a UserService and UserResource, where the UserService provides access to your database layer. 以您的情况为例,例如UserDao,或者您可以将其拆分为UserService和UserResource,其中UserService提供对数据库层的访问。 Up to you really. 真的取决于你。

Here is how you would implement this with DW integrated auth and how you would register this as well. 这是您将如何使用DW集成身份验证来实现此目的,以及如何进行注册。

In my example I am skipping the Hibernate aspect of this as it isn't too relevant. 在我的示例中,我跳过了它的休眠方面,因为它不太相关。 you can read about it here: http://www.dropwizard.io/1.0.2/docs/manual/hibernate.html 您可以在这里阅读有关内容: http : //www.dropwizard.io/1.0.2/docs/manual/hibernate.html

Here's my code: 这是我的代码:

public class AuthenticatorTest extends io.dropwizard.Application<Configuration> {

    @Override
    public void run(Configuration configuration, Environment environment) throws Exception {
        // register resource
        environment.jersey().register(MyHelloResource.class);

        // create the dao + dependencies
        UserDao dao = new UserDao(null); 

        // register new authenticator 
        environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<Principal>()
                .setAuthenticator(new UserAuth(dao)).setRealm("SUPER SECRET STUFF").buildAuthFilter()));

        // enables authentication via filter
        environment.jersey().register(RolesAllowedDynamicFeature.class);
    }

    public static void main(String[] args) throws Exception {
        new AuthenticatorTest().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
    }

    @Path("test")
    @Produces(MediaType.APPLICATION_JSON)
    public static class MyHelloResource {

        @GET
        @Path("asd")
        @PermitAll
        public String test(String x) {
            return "Hello";
        }

    }


    public static class UserAuth implements Authenticator<BasicCredentials, Principal> {
        private UserDao dao;

        public UserAuth(UserDao dao) {
            this.dao = dao;
        }

        @Override
        public Optional<Principal> authenticate(BasicCredentials credentials) throws AuthenticationException {
            String user = dao.getUser();
            return Optional.of(new Principal() {
                @Override
                public String getName() {
                    return user;
                }

            });
        }
    }

    public static class UserDao {

        private SessionFactory s;

        public UserDao(final SessionFactory s) {
            this.s = s;
        }

        public String getUser() {
            return "pandaadb";
        }
    }

}

And this is the breakdown of what we are doing. 这就是我们正在做的事情的分解。

First, as per docs, you would register your HibernateBundle within the bootstrap method as shown (in docs). 首先,按照文档,您将在所示的bootstrap方法内注册HibernateBundle (在docs中)。 This gives you access to the SessionFactory you require for your authentication. 这使您可以访问身份验证所需的SessionFactory

Your resource method will be annotated with a java security annotation. 您的资源方法将使用Java安全注释进行注释。 i am using PermitAll because I am disregarding roles. 我正在使用PermitAll因为我无视角色。

In the run method, you then create your DAO , register your resource and use the DW builder to add the required Filter and the Authenticator . 然后在run方法中,创建DAO ,注册资源并使用DW构建器添加所需的Filter和Authenticator This one specifically is for BasicCredentials , however there is nothing stopping you from doing any kind of filter for this. 这是专门针对BasicCredentials ,但是没有什么可以阻止您对此进行任何筛选。 DW already supports things like Ldap (in a different dependency), Basic Auth and so on. DW已经支持Ldap(具有不同的依赖性),Basic Auth等功能。

Now, since you create your beans in the run method, and you added your Hibernate bundle in the bootstrap method, you have access to the SessionFactory and can instantiate the DAO accordingly. 现在,由于您在run方法中创建了bean,并在bootstrap方法中添加了Hibernate捆绑包,因此您可以访问SessionFactory并可以相应地实例化DAO No need to have to pass it around. 无需传递它。

You also don't have to do any rest-request to access your user (though there is nothing stopping you adding that resource anyway, in case you need external access to it.) 您也不必执行任何其他休息请求即可访问您的用户(尽管万一您需要外部访问,也没有什么阻止您添加该资源的。)

So, to sum up, the important parts are: 因此,总而言之,重要的部分是:

  • Add A security annotation to your resource (eg PermitAll to allow all roles) 在您的资源中添加一个安全注释(例如, PermitAll以允许所有角色)
  • Add an authenticator implementation (in my case UserAuth ) that uses your DAO 添加使用您的DAO的身份验证器实现(在我的情况下为UserAuth
  • Instantiate it in the run method provided by dropwizard and register it with the jersey environment. 用dropwizard提供的run方法实例化它,并在jersey环境中注册它。

Note, this requires your user to implement the javax.security.Principal interface. 注意,这需要您的用户实现javax.security.Principal接口。 this is not a bad idea in general as a lot of security Frameworks make use of this. 通常,这并不是一个坏主意,因为许多安全框架都在使用此功能。

This, also, gives you more options with regards to DW. 这也为您提供了有关DW的更多选择。

You can add an Authorization implementation and a filter, and you'll be able to inject the User object into any resource method by adding an @Auth annotated object (see docs). 您可以添加一个Authorization实现和一个过滤器,并且可以通过添加带@Auth注释的对象(请参阅文档)将User对象注入到任何资源方法中。

Finally, the test of the standalone app from above: 最后,从上面对独立应用程序进行测试:

artur@pandaadb:~$ curl "localhost:9085/api/test/asd" -v 
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9085 (#0)
> GET /api/test/asd HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 401 Unauthorized
< Date: Mon, 17 Oct 2016 10:45:51 GMT
< WWW-Authenticate: Basic realm="SUPER SECRET STUFF"
< Content-Type: text/plain
< Content-Length: 49
< 
* Connection #0 to host localhost left intact
Credentials are required to access this resource.

artur@pandaadb:~$ curl "localhost:9085/api/test/asd" -H "Authorization: Basic dXNlcjpwYXNz" -v
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9085 (#0)
> GET /api/test/asd HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.47.0
> Accept: */*
> Authorization: Basic dXNlcjpwYXNz
> 
< HTTP/1.1 200 OK
< Date: Mon, 17 Oct 2016 10:46:11 GMT
< Content-Type: application/json
< Vary: Accept-Encoding
< Content-Length: 5
< 
* Connection #0 to host localhost left intact
Helloartur

I hope this helps you with your issue. 希望这对您的问题有所帮助。

Of course you don't need to use the DW method for Authentication. 当然,您不需要使用DW方法进行身份验证。 However I would recommend going down that road as you will have more support and a lot of things out of the box. 但是,我建议您沿着那条路走,因为您将获得更多的支持和很多其他东西。

However, the one thing you should rethink (if you don't use DW) is to not do a curl request to your Filter. 但是,您应该重新考虑的一件事(如果您不使用DW)是不对Filter进行卷曲请求。 Instead, instantiate it in the run method, and pass that instance to your Filter. 而是在run方法中实例化它,然后将该实例传递给您的Filter。

Note also, if you register your DAO with DW (as seen in hibernate docs), you will be able to use @Inject to inject your DAO into the Filter class that needs to use it. 还要注意,如果用DW注册DAO(如在休眠文档中所见),则可以使用@Inject将DAO注入需要使用它的Filter类中。

Right, I think that's all the info you need :) 是的,我认为这就是您需要的所有信息:)

Let me know if you have any problems, 如果您有任何问题,请告诉我,

Artur 阿图尔

Edit: 编辑:

I am doing an edit because I wrote a lot above and don't want to go over it. 我正在进行编辑,因为我在上面写了很多内容,并且不想遍历它。

I set up Hibernate to test this. 我设置了Hibernate进行测试。 The reason you are seeing issues is because the UnitOfWork is bound to the request scope. 您看到问题的原因是因为UnitOfWork已绑定到请求范围。 However, the resource annotations is matched AFTER the filter is invoked (since you need to do auth before executing the method). 但是,资源注释在调用过滤器后匹配(因为在执行方法之前需要进行身份验证)。 This is why you don't have a session. 这就是为什么您没有会议。

This is the solution. 这是解决方案。

In your run method, register a proxy for your auth implementation: 在您的run方法中,为您的auth实现注册一个代理:

UnitOfWorkAwareProxyFactory fac = new UnitOfWorkAwareProxyFactory(hibernate);

        UserAuth proxy = fac.create(UserAuth.class, UserDao.class, dao);

        environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<Principal>()
                .setAuthenticator(proxy).setRealm("SUPER SECRET STUFF").buildAuthFilter()));

This creates a proxy around the UserAuth class so that it is aware of the UnitOfWork annotation. 这将围绕UserAuth类创建一个代理,以便它了解UnitOfWork批注。

In your UserAuth class (or mine rather) you do: 在您的UserAuth类(或我的类)中,您可以执行以下操作:

public static class UserAuth implements Authenticator<BasicCredentials, Principal> {
        private UserDao dao;

        public UserAuth(UserDao dao) {
            this.dao = dao;
        }

        @Override
        @UnitOfWork
        public Optional<Principal> authenticate(BasicCredentials credentials) throws AuthenticationException {
            String user = dao.getUser();
            return Optional.of(new Principal() {
                @Override
                public String getName() {
                    return user;
                }

            });
        }
    }

Note the UnitOfWork annotation on the authenticate. 注意身份验证上的UnitOfWork批注。 This now opens a new session for you. 现在,这将为您打开一个新会话。 Please make sure to read up on UnitOfWork as it may have tricky side effects (or not) depending on how you use it. 请确保仔细阅读UnitOfWork,因为它可能会有(或没有)棘手的副作用,具体取决于您的使用方式。

Finally, this allowed my dao to talk to the database on an existing session. 最后,这允许我的dao在现有会话上与数据库进行对话。

Regards, 问候,

Artur 阿图尔

I finally, after much debugginf, find the proper way to open a session and execute a query. 最后,经过大量调试,我终于找到了打开会话并执行查询的正确方法。 I added this lines in Authenticator's authenticate mehod: 我在身份验证器的身份验证方法中添加了以下行:

 Session session = userDAO.getSessionFactory().openSession();
        Transaction transaction = session.getTransaction();
        Query userquery = session.createQuery("select u from User u where u.username = :username").setParameter("username", credentials.getUsername());
        List<com.amitbaz.tss.db.User> u = userquery.list();
        session.close();

And the same in Authorizater and it now works :) 和Authorizater中的一样,现在可以工作了:)

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

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