Demo project for Spring Boot 【1】spring-boot-starter【2】spring-boot-starter-security

在这里插入图片描述

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springsecuritytest1Application {

	public static void main(String[] args) {
		SpringApplication.run(Springsecuritytest1Application.class, args);
	}

}
/* 
 * 
addViewControllers()方法(重写WebMVCConfiguer中同名的方法)添加了四个视图控制器。
其中两个视图控制器引用名称为home(在中定义)的视图主页.html),
另一个引用名为hello的视图(在中定义)你好.html). 
第四个视图控制器引用另一个名为login的视图。您将在下一节中创建该视图。
此时,您可以跳到“运行应用程序”并运行应用程序,而无需登录任何内容。
现在您有了一个不安全的web应用程序,您可以为它添加安全性。 
 * 
 */
package com.example.securingweb;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/home").setViewName("home");
		registry.addViewController("/").setViewName("home");
		registry.addViewController("/hello").setViewName("hello");
		registry.addViewController("/login").setViewName("login");
	}

}
package com.example.securingweb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SecuringWebApplication {

	public static void main(String[] args) throws Throwable {
		SpringApplication.run(SecuringWebApplication.class, args);
	}

}
package com.example.securingweb;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.authorizeRequests()
				.antMatchers("/", "/home").permitAll()
				.anyRequest().authenticated()
				.and()
			.formLogin()
				.loginPage("/login")
				.permitAll()
				.and()
			.logout()
				.permitAll();
	}

	@Bean
	@Override
	public UserDetailsService userDetailsService() {
		UserDetails user =
			 User.withDefaultPasswordEncoder()
				.username("user")
				.password("password")
				.roles("USER")
				.build();

		return new InMemoryUserDetailsManager(user);
	}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
        <form th:action="@{/logout}" method="post">
            <input type="submit" value="Sign Out"/>
        </form>
    </body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example</title>
    </head>
    <body>
        <h1>Welcome!</h1>

        <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
    </body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/login}" method="post">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
    </body>
</html>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>springsecuritytest-1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springsecuritytest-1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

jdk1.8 jboss-eap-7.2 报错信息: ![图片说明](https://img-ask.csdn.net/upload/202005/14/1589443851_835180.png) ``` 2020-05-14 15:28:50,968 INFO [tk.mybatis.mapper.autoconfigure.MapperCacheDisabler] (ServerService Thread Pool -- 87) Clear tk.mybatis.mapper.version.VersionUtil CACHE cache. 2020-05-14 15:28:50,968 INFO [tk.mybatis.mapper.autoconfigure.MapperCacheDisabler] (ServerService Thread Pool -- 87) Clear EntityHelper entityTableMap cache. 2020-05-14 15:28:51,372 INFO [org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] (ServerService Thread Pool -- 87) Initializing ExecutorService 'taskScheduler' 2020-05-14 15:28:51,594 INFO [org.springframework.integration.endpoint.EventDrivenConsumer] (ServerService Thread Pool -- 87) Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel 2020-05-14 15:28:51,594 INFO [org.springframework.integration.channel.PublishSubscribeChannel] (ServerService Thread Pool -- 87) Channel 'application.errorChannel' has 1 subscriber(s). 2020-05-14 15:28:51,597 INFO [org.springframework.integration.endpoint.EventDrivenConsumer] (ServerService Thread Pool -- 87) started _org.springframework.integration.errorLogger 2020-05-14 15:28:51,599 INFO [org.springframework.scheduling.quartz.SchedulerFactoryBean] (ServerService Thread Pool -- 87) Starting Quartz Scheduler now 2020-05-14 15:28:51,663 INFO [org.springframework.scheduling.quartz.LocalDataSourceJobStore] (ServerService Thread Pool -- 87) ClusterManager: detected 1 failed or restarted instances. 2020-05-14 15:28:51,663 INFO [org.springframework.scheduling.quartz.LocalDataSourceJobStore] (ServerService Thread Pool -- 87) ClusterManager: Scanning for instance "DESKTOP-C8VU1M31589441150514"'s failed in-progress jobs. 2020-05-14 15:28:51,705 INFO [org.quartz.core.QuartzScheduler] (ServerService Thread Pool -- 87) Scheduler schedulerFactoryBean_$_DESKTOP-C8VU1M31589441328257 started. 2020-05-14 15:28:51,725 INFO [com.travelsky.ServletInitializer] (ServerService Thread Pool -- 87) Started ServletInitializer in 9.172 seconds (JVM running for 29.237) 2020-05-14 15:28:51,767 INFO [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 87) 初始化上下文 '/cscm' 的 Mojarra 2.3.5.SP2-redhat-00001 2020-05-14 15:28:58,000 SEVERE [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 87) Critical error during deployment: : java.lang.NullPointerException at javax.el.CompositeELResolver.add(CompositeELResolver.java:117) at com.sun.faces.el.DemuxCompositeELResolver.addRootELResolver(DemuxCompositeELResolver.java:142) at com.sun.faces.el.ELUtils.addEL3_0_Resolvers(ELUtils.java:336) at com.sun.faces.el.ELUtils.buildFacesResolver(ELUtils.java:258) at com.sun.faces.application.ApplicationAssociate.initializeELResolverChains(ApplicationAssociate.java:503) at com.sun.faces.application.ApplicationImpl.performOneTimeELInitialization(ApplicationImpl.java:1405) at com.sun.faces.application.ApplicationImpl.getELResolver(ApplicationImpl.java:529) at javax.faces.application.ApplicationWrapper.getELResolver(ApplicationWrapper.java:621) at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:256) at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187) at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:216) at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:185) at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42) at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43) at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:250) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:96) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:78) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35) at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985) at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487) at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378) at java.lang.Thread.run(Thread.java:748) at org.jboss.threads.JBossThread.run(JBossThread.java:485) 2020-05-14 15:28:58,004 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 87) MSC000001: Failed to start service jboss.deployment.unit."cscm.war".undertow-deployment: org.jboss.msc.service.StartException in service jboss.deployment.unit."cscm.war".undertow-deployment: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:81) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35) at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985) at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487) at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378) at java.lang.Thread.run(Thread.java:748) at org.jboss.threads.JBossThread.run(JBossThread.java:485) Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:252) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:96) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:78) ... 8 more Caused by: java.lang.RuntimeException: java.lang.NullPointerException at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:315) at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187) at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:216) at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:185) at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42) at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43) at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502) at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:250) ... 10 more Caused by: java.lang.NullPointerException at javax.el.CompositeELResolver.add(CompositeELResolver.java:117) at com.sun.faces.el.DemuxCompositeELResolver.addRootELResolver(DemuxCompositeELResolver.java:142) at com.sun.faces.el.ELUtils.addEL3_0_Resolvers(ELUtils.java:336) at com.sun.faces.el.ELUtils.buildFacesResolver(ELUtils.java:258) at com.sun.faces.application.ApplicationAssociate.initializeELResolverChains(ApplicationAssociate.java:503) at com.sun.faces.application.ApplicationImpl.performOneTimeELInitialization(ApplicationImpl.java:1405) at com.sun.faces.application.ApplicationImpl.getELResolver(ApplicationImpl.java:529) at javax.faces.application.ApplicationWrapper.getELResolver(ApplicationWrapper.java:621) at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:256) ... 21 more 2020-05-14 15:28:58,011 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: 操作("deploy") 失败 -- 地址:([("deployment" => "cscm.war")])-- 失败操作:{"WFLYCTL0080: 失败的服务" => {"jboss.deployment.unit.\"cscm.war\".undertow-deployment" => "java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException Caused by: java.lang.RuntimeException: java.lang.NullPointerException Caused by: java.lang.NullPointerException"}} 2020-05-14 15:28:58,019 INFO [org.jboss.as.server] (ServerService Thread Pool -- 42) WFLYSRV0010: 已部署的 "cscm.war"(runtime-name: "cscm.war") 2020-05-14 15:28:58,020 INFO [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: 服务状态报告 WFLYCTL0186: 启动失败的服务: service jboss.deployment.unit."cscm.war".undertow-deployment: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException 2020-05-14 15:28:58,058 INFO [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: 恢复服务器 2020-05-14 15:28:58,060 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: 侦听 http://0.0.0.0:9990/management 的 HTTP 管理接口 2020-05-14 15:28:58,060 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: 管理控制台侦听于 http://0.0.0.0:9990 2020-05-14 15:28:58,063 ERROR [org.jboss.as] (Controller Boot Thread) WFLYSRV0026: JBoss EAP 7.2.1.GA (WildFly Core 6.0.12.Final-redhat-00001) started (with errors) in 35479ms - Started 570 of 758 services (2 services failed or missing dependencies, 327 services are lazy, passive or on-demand) 2020-05-14 15:38:23,596 INFO [org.jboss.as.repository] (ServerService Thread Pool -- 94) WFLYDR0009: 内容 D:\jboss\jboss-eap-7.2\standalone\data ```
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页