参考書 「はじめてのJSP&サーブレット」で勉強しています。
windows10,Eclipse 4.6.3,MariaDB,HeidiSQLを使っています。
login.jspでform認証用に作成したログイン画面でログインできません。
ここまででしたことは、認証レルム設定を行っterapadserver.xmlの変更(2か所),/WEB-INF/web.xmlの変更、login.jspの追加です。
MariaDB/testデータベースにログインするための、テーブルをauth_roles,auth_usersを作成しています。
auth_rolesテーブルのデータ
|username|rolename|
|user01 |admin |
|user02 |role2 |
|user03 |role3 |
auth_usersテーブルのデータ
|username|password|
|user01 |password|
|user02 |password|
|user03 |password|
login.jspを実行するとログイン画面が表示されて、
userid:user01、password:passwordと打ち込むと、
ウェブ:http://localhost:8080/10_todo_425_442/j_security_checkで、
「ログインエラーです。」という表示がでてログインできません。
すべての./jsp/*,./servlet/*のファイルが同じ症状です。
エラーを見てserver.xml,tomcat-uses.xml,Context.xml,web.xmlをいろいろ値を変えてみたのですが。
うまくいきません。よろしくお願いいたします。
// server.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 ...中略... <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <!-- ここをコメントアウト <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/> --> <!-- ここを追加する --> <Resource auth="Container" defaultAutoCommit="false" driverClassName="org.mariadb.jdbc.Driver" initialSize="1" maxIdle="1" maxWaitMillis="-1" name="jdbc/authDB" password="password" type="javax.sql.DataSource" url="jdbc.mysql://127.0.0.1:3306/test" username="root"/> </GlobalNamingResources> ...中略... <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <!--ここをコメントアウト <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> --> <!-- ここを追加する --> <Realm className="org.apache.catalina.realm.DataSourceRealm" dataSourceName="jdbc/authDB" roleNameCol="rolename" userCredCol="password" userNameCol="username" userRoleTable="auth_roles" userTable="auth_users"/> </Realm> --> ...中略...
//WebContent/META-INF/context.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <Context> <Resource driverClassName="org.mariadb.jdbc.Driver" auth="Container" initialSize="1" maxIdle="1" maxWaitMillis="-1" name="jdbc/MariaDB" type="javax.sql.DataSource" url="jdbc:mysql://127.0.0.1:3306/test" username="root" password="password" defaultAutoCommit="false" /> <ResourceLink name="jdbc/MariaDB" //参考書:jdbc/authDBで結果は同じ表示です global="jdbc/MariaDB" //参考書:jdbc/authDBで結果は同じ表示です type="javax.sql.DataSource" /> </Context>
//WebContent/WEB-INF/web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>10_todo_443_452</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <%-- ここから下を追加しました--%> <session-config> <session-timeout>-1</session-timeout> </session-config> <security-role> <role-name>admin</role-name> </security-role> <security-role> <role-name>role2</role-name> </security-role> <security-constraint> <web-resource-collection> <web-resource-name>FORM認証</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> <role-name>role2</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <realm-name>DataSourceRealm</realm-name> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/login_error.jsp</form-error-page> </form-login-config> </login-config> </web-app>
//login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
</form> </body> </html> ```<form method="post" action="${fn:escapeXml('j_security_check')}"> <table> <tr> <td>ユーザーID</td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>パスワード</td> <td><input type="password" name="j_password"></td> </tr> </table><br> <input type="submit" value="ログイン" name="submit"> <input type="reset" value="リセット" name="reset">
//Eclipseのコンソール抜粋 9 06, 2019 8:44:17 午前 org.apache.catalina.startup.Catalina start 情報: Server startup in 1415 ms 9 06, 2019 8:44:31 午前 org.apache.catalina.realm.DataSourceRealm open 重大: 認証を実行中の例外です javax.naming.NameNotFoundException: 名前 [jdbc/MariaDB] はこのコンテキストにバインドされていません at org.apache.naming.NamingContext.lookup(NamingContext.java:816) at org.apache.naming.NamingContext.lookup(NamingContext.java:173) at org.apache.catalina.realm.DataSourceRealm.open(DataSourceRealm.java:393) at org.apache.catalina.realm.DataSourceRealm.authenticate(DataSourceRealm.java:263) at org.apache.catalina.realm.CombinedRealm.authenticate(CombinedRealm.java:195) at org.apache.catalina.realm.LockOutRealm.authenticate(LockOutRealm.java:158) at org.apache.catalina.authenticator.FormAuthenticator.doAuthenticate(FormAuthenticator.java:264) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:575) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/29 05:50
退会済みユーザー
2019/08/29 06:37
退会済みユーザー
2019/09/04 13:02
退会済みユーザー
2019/09/06 00:04