您的位置:首页 > 其它

CAS之5.2x版本自定义错误信息-yellowcong

2018-02-02 23:18 375 查看
cas登录界面,有的消息,不是我们想看到的,所以我们可以通过自定义消息,来解决这个问题。实现cas错误消息定制,需要有3个步骤,1、创建 一个异常类继承javax.security.auth.login.AccountExpiredException ,2、配置异常到application.properties中,3、在messages_zh_CN.properties 配置文件中,配置异常弹出的消息,4、配置pom.xml文件,设置将自定义的properites配置文件打包进去。(我就没有设定,自己把自己坑了一把啊)

项目代码

https://gitee.com/yellowcong/springboot_cas/tree/master/cas-server-exception


项目结构

主要添加了连个异常类,然后修改了



自定义异常类

1、创建异常类

异常类,需要继承AccountExpiredException这个类,然后需要复写够着方法。

package com.yellowcong.auth.exception;

import javax.security.auth.login.AccountExpiredException;

public class CustomException1 extends AccountExpiredException{

/**
*
*/
private static final long serialVersionUID = 1L;

public CustomException1() {
super();
// TODO Auto-generated constructor stub
}

public CustomException1(String msg) {
super(msg);
// TODO Auto-generated constructor stub
}

}


2、配置application.properties

配置两个Exception,多个exception的时候,用逗号分割开

cas.authn.exceptions.exceptions= com.yellowcong.auth.exception.CustomException1,com.yellowcong.auth.exception.CustomException2


完整配置

##
# CAS Server Context Configuration
#
#设定项目的目录 / 表示根目录
server.context-path=/
#端口号
server.port=9000

#SSL配置 开启https
server.ssl.enabled=true
server.ssl.key-store=classpath:tomcat.keystore
server.ssl.key-store-password=yellowcong
#查看别名,别名不是瞎写的
#keytool -list -keystore D:/tomcat.keystore
server.ssl.keyAlias=tomcat

cas.tgc.secure=false
cas.warningCookie.secure=false

cas.authn.exceptions.exceptions=com.yellowcong.auth.exception.CustomException1,com.yellowcong.auth.exception.CustomException2
#设置不实用ssl

server.max-http-header-size=2097152
server.use-forward-headers=true
server.connection-timeout=20000
server.error.include-stacktrace=ALWAYS

server.compression.enabled=true
server.comprsession.mime-types=application/javascript,application/json,application/xml,text/html,text/xml,text/plain

server.tomcat.max-http-post-size=2097152
server.tomcat.basedir=build/tomcat
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)
server.tomcat.accesslog.suffix=.log
server.tomcat.max-threads=10
server.tomcat.port-header=X-Forwarded-Port
server.tomcat.protocol-header=X-Forwarded-Proto
server.tomcat.protocol-header-https-value=https
server.tomcat.remote-ip-header=X-FORWARDED-FOR
server.tomcat.uri-encoding=UTF-8

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

##
# CAS Cloud Bus Configuration
#
spring.cloud.bus.enabled=false

endpoints.enabled=false
endpoints.sensitive=true

endpoints.restart.enabled=false
endpoints.shutdown.enabled=false

management.security.enabled=true
management.security.roles=ACTUATOR,ADMIN
management.security.sessions=if_required
management.context-path=/status
management.add-application-context-header=false

security.basic.authorize-mode=role
security.basic.enabled=false
security.basic.path=/status/**

##
# CAS Web Application Session Configuration
#
server.session.timeout=300
server.session.cookie.http-only=true
server.session.tracking-modes=COOKIE

##
# CAS Thymeleaf View Configuration
#
spring.thymeleaf.encoding=UTF-8
#修改设定为不缓存,默认是有缓存的
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML

##
# CAS Log4j Configuration
#
server.context-parameters.isLog4jAutoInitializationDisabled=true

##
# CAS AspectJ Configuration
#
spring.aop.auto=true
spring.aop.proxy-target-class=true

##
# CAS Authentication Credentials
#
#cas.authn.accept.users=yellowcong::yellowcong
#记住我
cas.ticket.tgt.rememberMe.enabled=true
cas.ticket.tgt.rememberMe.timeToKillInSeconds=3600

#退出后转发到对应的service
cas.logout.followServiceRedirects=true

cas.serviceRegistry.initFromJson=true


3、配置messages_zh_CN.properties

在messages_zh_CN.properties 中配置需要提示的消息

authenticationFailure.CustomException1=自定义错误1。
authenticationFailure.CustomException2=自定义错误2。


4、使用异常信息

异常信息,需要在自定义的处理类中,添加处理。自定义登录处理,可以查看我的另一个教程: CAS之5.2x版本自定义登录,多数据源登录-yellowcong

package com.yellowcong.auth.handler;

import java.security.GeneralSecurityException;
import java.util.Collections;

import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.FailedLoginException;

import org.apereo.cas.authentication.Credential;
import org.apereo.cas.authentication.HandlerResult;
import org.apereo.cas.authentication.PreventedException;
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.apereo.cas.authentication.exceptions.AccountDisabledException;
import org.apereo.cas.authentication.exceptions.InvalidLoginLocationException;
import org.apereo.cas.authentication.handler.support.AbstractPreAndPostProcessingAuthenticationHandler;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.services.ServicesManager;

import com.yellowcong.auth.exception.CustomException1;
import com.yellowcong.auth.exception.CustomException2;
/**
* @author yellowcong
* 创建日期:2018/02/02
*
*/
public class CustomerHandler extends AbstractPreAndPostProcessingAuthenticationHandler {

public CustomerHandler(String name, ServicesManager servicesManager, PrincipalFactory principalFactory,
Integer order) {
super(name, servicesManager, principalFactory, order);
}

/**
* 用于判断用户的Credential(换而言之,就是登录信息),是否是俺能处理的
* 就是有可能是,子站点的登录信息中不止有用户名密码等信息,还有部门信息的情况
*/
@Override
public boolean supports(Credential credential) {
//判断传递过来的Credential 是否是自己能处理的类型
return credential instanceof UsernamePasswordCredential;
}

/**
* 用于授权处理
*/
@Override
protected HandlerResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {
UsernamePasswordCredential usernamePasswordCredentia = (UsernamePasswordCredential) credential;

//获取传递过来的用户名和密码
String username = usernamePasswordCredentia.getUsername();
String password = usernamePasswordCredentia.getPassword();

if(username.equals("doubi1")){
//doubi1 的时候,抛出 异常信息1
//异常信息1
throw new CustomException1();
}else if(username.equals("doubi2")){
//doubi2 的时候,抛出异常信息2
//异常信息2
throw new CustomException2();
}else if(username.startsWith("admin")) {
//当是admin用户的情况,直接就登录了,谁叫他是admin用户呢
//直接返回去了
return createHandlerResult(credential, this.principalFactory.createPrincipal(username, Collections.emptyMap()), null);
}else if (username.startsWith("lock")) {
//用户锁定
throw new AccountLockedException();
} else if (username.startsWith("disable")) {
//用户禁用
throw new AccountDisabledException();
} else if (username.startsWith("invali")) {
//禁止登录该工作站登录
throw new InvalidLoginLocationException();
} else if (username.startsWith("passorwd")) {
//密码错误
throw new FailedLoginException();
} else if (username.startsWith("account")) {
//账号错误
throw new AccountLockedException();
}
return null;
}

}


5、配置pom.xml 配置,添加自定义message

一定要配置使用当先的property的国际化文件,不然找不到异常信息,我就干过这蠢事,就是找不到异常信息

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<warName>cas</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
<recompressZippedFiles>false</recompressZippedFiles>
<archive>
<compress>false</compress>
<manifestFile>${manifestFileToUse}</manifestFile>
</archive>
<overlays>
<overlay>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp${app.server}</artifactId>
<!--原有的服务不再初始化进去-->
<excludes>
<!--<exclude>WEB-INF/classes/services/*</exclude>-->
<!--<exclude>WEB-INF/classes/application.*</exclude>-->
<!--<exclude>WEB-INF/classes/bootstrap.properties</exclude>-->
<!--<exclude>WEB-INF/classes/apereo.properties</exclude>-->
<!--<exclude>**/user-details.properties</exclude>-->
<exclude>**/messages_*.properties</exclude>
<exclude>**/truststore.jks</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>


pom.xml完整配置

<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-exception</artifactId>
<packaging>war</packaging>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp${app.server}</artifactId>
<version>${cas.version}</version>
<type>war</type>
<scope>system</scope>
<optional>true</optional>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cas-server-webapp-tomcat-5.2.1.war</systemPath>
</dependency>

<!-- 自定义认证的方式 begin -->
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-webflow</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-authentication</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp-config</artifactId>
<version>${cas.version}</version>
<scope>provided</scope>
</dependency>

<!-- MYSQL -->
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- Spring Boot JDBC -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>1.5.10.RELEASE</version>
</dependency> -->

<!--新增支持jdbc验证-->
<!-- <dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${cas.version}</version>
</dependency> -->
<!--自定义登录-->
<!-- <dependency>
<groupId>com.air</groupId>
<artifactId>security-support-authentication</artifactId>
<version>${project.version}</version>
</dependency> -->
<!--
若不想找驱动可以直接写下面的依赖即可,其中包括
HSQLDB、Oracle、MYSQL、PostgreSQL、MariaDB、Microsoft SQL Server
-->
<!-- <dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-jdbc-drivers</artifactId>
<version>${cas.version}</version>
</dependency>-->
<!--
<dependency>
<groupId>com.oracle</groupId>
<artifactId>jdbc-driver</artifactId>
<systemPath>${basedir}/libs/ojdbc7.jar</systemPath>
<scope>system</scope>
<version>12c</version>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.rimerosolutions.maven.plugins</groupId>
<artifactId>wrapper-maven-plugin</artifactId>
<version>0.0.5</version>
<configuration>
<verifyDownload>true</verifyDownload>
<checksumAlgorithm>MD5</checksumAlgorithm>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
<configuration>
<mainClass>${mainClassName}</mainClass>
<addResources>true</addResources>
<executable>${isExecutable}</executable>
<layout>WAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
<includeScope>system</includeScope>
</configuration>
</execution>
</executions>
</plugin>

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration> <warName>cas</warName> <failOnMissingWebXml>false</failOnMissingWebXml> <recompressZippedFiles>false</recompressZippedFiles> <archive> <compress>false</compress> <manifestFile>${manifestFileToUse}</manifestFile> </archive> <overlays> <overlay> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-webapp${app.server}</artifactId> <!--原有的服务不再初始化进去--> <excludes> <!--<exclude>WEB-INF/classes/services/*</exclude>--> <!--<exclude>WEB-INF/classes/application.*</exclude>--> <!--<exclude>WEB-INF/classes/bootstrap.properties</exclude>--> <!--<exclude>WEB-INF/classes/apereo.properties</exclude>--> <!--<exclude>**/user-details.properties</exclude>--> <exclude>**/messages_*.properties</exclude> <exclude>**/truststore.jks</exclude> </excludes> </overlay> </overlays> </configuration> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
</plugin>
</plugins>
<finalName>cas</finalName>
</build>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<cas.version>5.2.1</cas.version>
<springboot.version>1.5.9.RELEASE</springboot.version>
<spring.version>4.3.14.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<servlet.version>3.1.0</servlet.version>
<java.cas.client.version>3.5.0</java.cas.client.version>
<!-- app.server could be -jetty, -undertow, -tomcat, or blank if you plan to provide appserver -->
<app.server>-tomcat</app.server>
<mainClassName>org.springframework.boot.loader.WarLauncher</mainClassName>
<isExecutable>false</isExecutable>
<manifestFileToUse>
${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF
</manifestFileToUse>
</properties>

<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>exec</id>
<properties>
<mainClassName>org.apereo.cas.web.CasWebApplication</mainClassName>
<isExecutable>true</isExecutable>
<manifestFileToUse></manifestFileToUse>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>echo-maven-plugin</artifactId>
<version>0.3.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
<configuration>
<echos>
<echo>Executable profile to make the generated CAS web application executable.</echo>
</echos>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<!--
支持API文档
Swagger API Specification /v2/api-docs
Swagger UI /swagger-ui.html
-->
<id>dev</id>
<dependencies>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-documentation-swagger</artifactId>
<version>${cas.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>


6、启动服务

在项目目录,启动cas服务

build.cmd debug




测试异常

使用doubi1用户登录测试



使用doub2用户测试

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: