提交 fc861a41 编写于 作者: 智布道's avatar 智布道 👁

🎨 Add core module code, define policy interface and user interface, and...

🎨 Add core module code, define policy interface and user interface, and provide exception classes
上级 3e8527d7
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.fujieid</groupId>
<artifactId>jap</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jap-core</artifactId>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
</dependency>
</dependencies>
</project>
package com.fujieid.jap.core;
/**
* Jap configuration.
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 14:19
* @since 1.0.0
*/
public class JapConfig {
/**
* Save login state in session, defaults to {@code true}
*/
private boolean session;
/**
* After successful login, redirect to {@code successRedirect}. Default is `/`
*/
private String successRedirect = "/";
/**
* Prompt message after successful login
*/
private String successMessage;
/**
* After failed login, redirect to {@code failureRedirect}. Default is `/error`
*/
private String failureRedirect = "/error";
/**
* Prompt message after login failed
*/
private String failureMessage;
public boolean isSession() {
return session;
}
public JapConfig setSession(boolean session) {
this.session = session;
return this;
}
public String getSuccessRedirect() {
return successRedirect;
}
public JapConfig setSuccessRedirect(String successRedirect) {
this.successRedirect = successRedirect;
return this;
}
public String getSuccessMessage() {
return successMessage;
}
public JapConfig setSuccessMessage(String successMessage) {
this.successMessage = successMessage;
return this;
}
public String getFailureRedirect() {
return failureRedirect;
}
public JapConfig setFailureRedirect(String failureRedirect) {
this.failureRedirect = failureRedirect;
return this;
}
public String getFailureMessage() {
return failureMessage;
}
public JapConfig setFailureMessage(String failureMessage) {
this.failureMessage = failureMessage;
return this;
}
}
package com.fujieid.jap.core;
/**
* JAP constant
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 18:19
* @since 1.0.0
*/
public interface JapConst {
String SESSION_USER_KEY = "_jap:session:user";
}
package com.fujieid.jap.core;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 14:48
* @since 1.0.0
*/
public class JapUser {
private String userId;
private String username;
private String password;
public String getUserId() {
return userId;
}
public JapUser setUserId(String userId) {
this.userId = userId;
return this;
}
public String getUsername() {
return username;
}
public JapUser setUsername(String username) {
this.username = username;
return this;
}
public String getPassword() {
return password;
}
public JapUser setPassword(String password) {
this.password = password;
return this;
}
}
package com.fujieid.jap.core;
import com.fujieid.jap.core.exception.JapUserException;
/**
* Abstract the user-related function interface, which is implemented by the caller business system.
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 14:21
* @since 1.0.0
*/
public interface JapUserService {
/**
* Get user info by userid.
*
* @param userId User id of the business system
* @return JapUser
*/
default JapUser getById(String userId) {
throw new JapUserException("JapUserService#getById(String) must be overridden by subclass");
}
/**
* Get user info by username.
*
* @param username username of the business system
* @return JapUser
*/
default JapUser getByName(String username) {
throw new JapUserException("JapUserService#getByName(String) must be overridden by subclass");
}
/**
* Verify that the password entered by the user matches
*
* @param password The password in the HTML-based login form
* @param user User information that is queried by the user name in the HTML form
* @return {@code boolean} When true is returned, the password matches, otherwise the password is wrong
*/
default boolean validPassword(String password, JapUser user) {
throw new JapUserException("JapUserService#getByName(String) must be overridden by subclass");
}
}
package com.fujieid.jap.core.exception;
/**
* Wrap all exception information related to the JAP
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 14:50
* @since 1.0.0
*/
public class JapException extends RuntimeException {
/**
* Constructs a new runtime exception with {@code null} as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to {@link #initCause}.
*/
public JapException() {
}
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public JapException(String message) {
super(message);
}
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public JapException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new runtime exception with the specified cause and a
* detail message of <tt>(cause==null ? null : cause.toString())</tt>
* (which typically contains the class and detail message of
* <tt>cause</tt>). This constructor is useful for runtime exceptions
* that are little more than wrappers for other throwables.
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public JapException(Throwable cause) {
super(cause);
}
/**
* Constructs a new runtime exception with the specified detail
* message, cause, suppression enabled or disabled, and writable
* stack trace enabled or disabled.
*
* @param message the detail message.
* @param cause the cause. (A {@code null} value is permitted,
* and indicates that the cause is nonexistent or unknown.)
* @param enableSuppression whether or not suppression is enabled
* or disabled
* @param writableStackTrace whether or not the stack trace should
* be writable
* @since 1.7
*/
public JapException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
package com.fujieid.jap.core.exception;
/**
* Wrap all exception information related to the JAP Strategies
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 14:32
* @since 1.0.0
*/
public class JapStrategyException extends JapException {
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public JapStrategyException(String message) {
super(message);
}
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public JapStrategyException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new runtime exception with the specified cause and a
* detail message of <tt>(cause==null ? null : cause.toString())</tt>
* (which typically contains the class and detail message of
* <tt>cause</tt>). This constructor is useful for runtime exceptions
* that are little more than wrappers for other throwables.
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public JapStrategyException(Throwable cause) {
super(cause);
}
}
package com.fujieid.jap.core.exception;
/**
* Wrap all exception information related to the JAP User
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 14:49
* @since 1.0.0
*/
public class JapUserException extends JapException {
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public JapUserException(String message) {
super(message);
}
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public JapUserException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new runtime exception with the specified cause and a
* detail message of <tt>(cause==null ? null : cause.toString())</tt>
* (which typically contains the class and detail message of
* <tt>cause</tt>). This constructor is useful for runtime exceptions
* that are little more than wrappers for other throwables.
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public JapUserException(Throwable cause) {
super(cause);
}
}
package com.fujieid.jap.core.strategy;
import com.fujieid.jap.core.JapConfig;
import com.fujieid.jap.core.exception.JapStrategyException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* The unified implementation interface of JAP Strategy, which must be implemented for all specific business policies.
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @date 2021/1/11 14:27
* @since 1.0.0
*/
public interface JapStrategy {
/**
* This function must be overridden by subclasses. In abstract form, it always throws an exception.
*
* @param config Jap Configs
* @param request The request to authenticate
* @param response The response to authenticate
*/
default void authenticate(JapConfig config, HttpServletRequest request, HttpServletResponse response) {
throw new JapStrategyException("JapStrategy#authenticate must be overridden by subclass");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册