3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Tomcat 認証 (Struts2 Form)

Last updated at Posted at 2014-01-22

メモ

部分部分が多すぎなため。まとめ

server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server
	port="8105"
	shutdown="SHUTDOWN">

	<Listener
		SSLEngine="on"
		className="org.apache.catalina.core.AprLifecycleListener" />

	<Listener className="org.apache.catalina.core.JasperListener" />
	<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
	<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
	<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

	<GlobalNamingResources>
		<Resource
			auth="Container"
			factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
			name="UserDatabase"
			pathname="conf/tomcat-users.xml"
			type="org.apache.catalina.UserDatabase" />
	</GlobalNamingResources>
	.
	<Service name="Catalina">

		<Connector
			connectionTimeout="20000"
			port="8180"
			protocol="HTTP/1.1"
			redirectPort="8443" />


		<Engine
			defaultHost="localhost"
			name="Catalina">

			<Realm
				className="org.apache.catalina.realm.UserDatabaseRealm"
				resourceName="UserDatabase" />

			<Host
				appBase="webapps"
				autoDeploy="true"
				name="localhost"
				unpackWARs="false">
				<Context
					docBase="struts2.app"
					path="/struts2.app"
					reloadable="true"
					source="org.eclipse.jst.j2ee.server:struts2.app" />
			</Host>
		</Engine>
	</Service>
</Server>
web.xml
<?xml version="1.0"?>
<web-app
	version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>strtus2 app</display-name>
	<filter>
		<description>struts2 action filter</description>
		<display-name>struts2</display-name>
		<filter-name>struts2</filter-name>
		<filter-class>
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
		<init-param>
			<description>action class apckages</description>
			<param-name>actionPackages</param-name>
			<param-value>action</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>/index.html</welcome-file>
	</welcome-file-list>
	<jsp-config>
		<jsp-property-group>
			<url-pattern>*.jsp</url-pattern>
			<page-encoding>UTF-8</page-encoding>
			<include-prelude>/include/header.jspf</include-prelude>
		</jsp-property-group>
	</jsp-config>
	<security-constraint>
		<display-name>admin</display-name>
		<web-resource-collection>
			<web-resource-name>admin</web-resource-name>
			<url-pattern>/*</url-pattern>
		</web-resource-collection>
	</security-constraint>
	<login-config>
		<auth-method>BASIC</auth-method>
		<realm-name>UserDatabase</realm-name>
	</login-config>
</web-app>
IndexAction
import java.util.Map;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.ScopedModelDriven;

@ParentPackage("struts-default")
@Namespaces({
		@Namespace("")
})
@Actions({
		@Action("index")
})
@Results({
		@Result(location = "index.html"),
		@Result(name = ActionSupport.SUCCESS, location = "index.html"),
		@Result(name = ActionSupport.ERROR, location = "index.html"),
		@Result(name = ActionSupport.INPUT, location = "index.html"),
})
public class IndexAction extends ActionSupport implements //
		ScopedModelDriven<IndexModel>, SessionAware, ServletRequestAware, ServletResponseAware {

	static final Logger log = Logger.getLogger(IndexAction.class);

	IndexModel model;

	String scopeKey;

	Map<String, Object> session;

	HttpServletRequest request;

	HttpServletResponse response;

	@Override
	public IndexModel getModel() {
		try {
			return Objects.requireNonNull(model);
		} catch (NullPointerException e) {
			this.model = new IndexModel();
			return this.model;
		} finally {
			if (log.isDebugEnabled()) {
				log.debug("model    : " + model.toString());
			}
		}
	}

	@Override
	public void setModel(IndexModel model) {
		this.model = model;
	}

	@Override
	public String getScopeKey() {
		return scopeKey;
	}

	@Override
	public void setScopeKey(String scopeKey) {
		if (log.isDebugEnabled()) {
			log.debug("scopeKey : " + scopeKey);
		}
		this.scopeKey = scopeKey;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public String execute() throws Exception {
		log.info("name : " + ActionContext.getContext().getName());

		try {
			request.login(model.userId, model.password);

			log.debug(LOGIN);
			return LOGIN;
		} catch (ServletException e) {
			log.warn(e.getMessage());
		}

		log.debug(INPUT);
		return INPUT;
	}

}
IndexModel

import java.io.Serializable;

import net.sf.oval.constraint.NotEmpty;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.log4j.Logger;

public class IndexModel implements Serializable {
	static final Logger log = Logger.getLogger(IndexModel.class);

	@NotEmpty(message = "1")
	public String userId;

	@NotEmpty(message = "2")
	public String password;

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?