博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
configure JDBCRealm JAAS for mysql and tomcat 7 with form based authentication--reference
阅读量:6616 次
发布时间:2019-06-24

本文共 6807 字,大约阅读时间需要 22 分钟。

Hello all,

In this tutorial we are going to configure JDBCRealm JAAS for tomcat 7 and mysql database server.

Let us first understand what exactly these terminologies mean.

JAAS : Java Authentication and Authorization Service is used for user authentication. This provides separation of concerns for user authentication so that they are managed independently

JDBCRealm: We can say this is used to look for users in provided relational database. All the user credentials will be retrieved by tomcat using JDBCRealm.

Form based authentication: This is a mechanism by which security is provided for web resources. If the user is authenticated, then resource will be served, otherwise it will lead to a login page where user can fill in login credentials and after successful login, the resource will be served.

We will follow these steps for configuration

1. Prepare database for user credentials and roles

2. Configure tomcat 7 server for JDBCRealm with our database

3. Create a web application in eclipse

4. Configure security for the resources which we want to protect in our web application

5. Run example

1. Prepare database:

Copy paste the following sql script and run from mysql command prompt

This will create the database and add data into it.

2. Configure tomcat 7 server.xml for JDBCRealm

Add a realm tag in tomcat_home/conf/server.xml file. Place mysql-connector-java.jar in tomcat_home/lib

 

 

3. Create a dynamic web project in eclipse.

Click File -> New -> Dynamic Web Project. Name it Tomcat7FormBasedJAAS. Also place mysql-connector-java.jar in WEB-INF/lib

Directory structure after creating project

4. Configure security for web application

Paste following content in web.xml

 

 

Create protected.jsp in Webcontent/protected folder and paste following code in it.

 

 

Create login.jsp as follows

 

 

Note that the names for username, password and action must be j_username, j_password and j_security_check. It means we are using JAAS

Create a page if user authentication fails. Name it login_failure.jsp

 

 

5. Run and test the application

Start tomcat 7 server and hit

 

 

Now that we are trying to access a protected file based on url pattern, the container will take us to the login page as per our configuration in web.xml

The following page should be displayed when you hit above url.

Login Page

Now enter wrong credentials. say I enter username as prasadkharkar and password as 1234.

Now container will check whether these match the credentials specified in the database. If they don’t match it will redirect you to error page as follows

Login Error

 

When you enter correct credentials..i.e. username as “prasadkharkar” and password as “password”. Then you will be successfully redirected to the protected resource that you are trying to access because now you are a authenticated user.

Successful Login

reference from:

http://www.thejavageek.com/2013/07/07/configure-jdbcrealm-jaas-for-mysql-and-tomcat-7-with-form-based-authentication/

 

 

转载地址:http://ymhso.baihongyu.com/

你可能感兴趣的文章
关于IT服务管理的服务台
查看>>
rundeck 修改密码 添加节点
查看>>
IT讲师韩顺平:创业不易,尚硅谷延续教育初心
查看>>
IntelliJ IDEA 插件 阿里巴巴Java开发手册
查看>>
利用nmap对Mongodb Redis未授权访问测试
查看>>
CakePHP
查看>>
我的友情链接
查看>>
编译mysql5.6.27
查看>>
搭建centos6.7网站服务器记录
查看>>
Release版本调用ffmpeg av_register_all程序崩溃
查看>>
Referenced management pack not found
查看>>
jquery中data函数的用法示例
查看>>
巧用strtotime函数计算日期
查看>>
JVM中java对象的生命周期
查看>>
mysql 查看连接数,状态
查看>>
JFinal集成YUI Compressor压缩合并JS和CSS
查看>>
windows下的Oracle卸载
查看>>
sqlserver查看死锁的存储过程
查看>>
在VirtualBox中的CentOS 6.3下安装VirtualBox增强包(GuestAd...
查看>>
Java开发中的23种设计模式详解(转)
查看>>