简体   繁体   中英

how to do authenticate in REST web service using jersey and java

how to do authentication in REST web service using jersey framework and java?I used NetBean IDE and create Rest web service, the application server is glassfish. I don't use javadb or derby. My database is mongodb. I have google and i found http://weblogs.java.net/blog/mhadley/archive/2008/03/authentication.html . But that site described he used derby database. How should i configure in web.xml for authenticate?

You can as well use file realm of glassfish to test if your code & basic authentication works. For that add your users into file realm and modify your configuration in web.xml:

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>file</realm-name>
</login-config>
<security-role>
    <description/>
    <role-name>USERS</role-name>
</security-role>

Later you can switch to jdbc realm and add jdbc data source to use with mongodb and realm.

The page you link to will let you make authentication work, even if your application uses another db. You would then have user credentials in a separate db (derby in that case, but you are free to use any db for which you have a jdbc driver).

You could also use another 'realm' for authentication. If you just have a couple of users and don't need to change their username/password from application code, you could just use 'filerealm' in glassfish.

You could also design your own realm, but that's more work. You could find some blog entries about that, or just find some inspiration in Glassfish source code.

I don't think you can use the referenced 'jdbcrealm' for mongodb, as I don't think there is a full jdbc driver for it (as that probably wouldn't make sense, mongodb being a document-oriented database and not a relational one). If you really need your users and groups to be defined in mongodb, you should write a custom realm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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