Skip to content

Commit c1e6dd7

Browse files
committed
m
1 parent 288c8bf commit c1e6dd7

File tree

16 files changed

+827
-0
lines changed

16 files changed

+827
-0
lines changed

form-login-3/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.5.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>org.javaboy</groupId>
12+
<artifactId>form-login-3</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>form-login-3</name>
15+
<description>公众号:江南一点雨</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-security</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
<exclusions>
36+
<exclusion>
37+
<groupId>org.junit.vintage</groupId>
38+
<artifactId>junit-vintage-engine</artifactId>
39+
</exclusion>
40+
</exclusions>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.security</groupId>
44+
<artifactId>spring-security-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.javaboy.formlogin;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class FormLoginApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(FormLoginApplication.class, args);
11+
}
12+
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.javaboy.formlogin;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
/**
8+
* @作者 江南一点雨
9+
* @公众号 江南一点雨
10+
* @微信号 a_java_boy
11+
* @GitHub https://github.com/lenve
12+
* @博客 http://wangsong.blog.csdn.net
13+
* @网站 http://www.javaboy.org
14+
*/
15+
@RestController
16+
public class HelloController {
17+
@GetMapping("/hello")
18+
public String hello() {
19+
return "hello";
20+
}
21+
22+
@RequestMapping("/hello1")
23+
public String hello1() {
24+
return "hello1";
25+
}
26+
@RequestMapping("/hello2")
27+
public String hello2() {
28+
return "hello2";
29+
}
30+
31+
@RequestMapping("/f1")
32+
public String f1() {
33+
return "f1";
34+
}
35+
@RequestMapping("/f2")
36+
public String f2() {
37+
return "f2";
38+
}
39+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package org.javaboy.formlogin;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.authentication.AuthenticationManager;
7+
import org.springframework.security.authentication.InsufficientAuthenticationException;
8+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
9+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
10+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
11+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
12+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
13+
import org.springframework.security.core.Authentication;
14+
import org.springframework.security.core.AuthenticationException;
15+
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
16+
import org.springframework.security.crypto.password.PasswordEncoder;
17+
import org.springframework.security.web.AuthenticationEntryPoint;
18+
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
19+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
20+
21+
import javax.servlet.ServletException;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
import java.io.IOException;
25+
import java.io.PrintWriter;
26+
27+
/**
28+
* @作者 江南一点雨
29+
* @公众号 江南一点雨
30+
* @微信号 a_java_boy
31+
* @GitHub https://github.com/lenve
32+
* @博客 http://wangsong.blog.csdn.net
33+
* @网站 http://www.javaboy.org
34+
*/
35+
@Configuration
36+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
37+
@Bean
38+
PasswordEncoder passwordEncoder() {
39+
return NoOpPasswordEncoder.getInstance();
40+
}
41+
42+
@Override
43+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
44+
auth.inMemoryAuthentication()
45+
.withUser("javaboy")
46+
.password("123").roles("admin");
47+
}
48+
49+
@Override
50+
public void configure(WebSecurity web) throws Exception {
51+
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
52+
}
53+
54+
@Override
55+
protected void configure(HttpSecurity http) throws Exception {
56+
http.authorizeRequests()
57+
.anyRequest().authenticated()
58+
.and()
59+
.formLogin()
60+
.loginPage("/login.html")
61+
.loginProcessingUrl("/doLogin")
62+
.usernameParameter("name")
63+
.passwordParameter("passwd")
64+
.successHandler((req, resp, authentication) -> {
65+
Object principal = authentication.getPrincipal();
66+
resp.setContentType("application/json;charset=utf-8");
67+
PrintWriter out = resp.getWriter();
68+
out.write(new ObjectMapper().writeValueAsString(principal));
69+
out.flush();
70+
out.close();
71+
})
72+
.failureHandler((req, resp, e) -> {
73+
resp.setContentType("application/json;charset=utf-8");
74+
PrintWriter out = resp.getWriter();
75+
out.write(e.getMessage());
76+
out.flush();
77+
out.close();
78+
})
79+
.permitAll()
80+
.and()
81+
.logout()
82+
.logoutUrl("/logout")
83+
.logoutSuccessHandler((req, resp, authentication) -> {
84+
resp.setContentType("application/json;charset=utf-8");
85+
PrintWriter out = resp.getWriter();
86+
out.write("注销成功");
87+
out.flush();
88+
out.close();
89+
})
90+
.permitAll()
91+
.and()
92+
.csrf().disable().exceptionHandling()
93+
.authenticationEntryPoint((req, resp, authException) -> {
94+
resp.setContentType("application/json;charset=utf-8");
95+
PrintWriter out = resp.getWriter();
96+
out.write("尚未登录,请先登录");
97+
out.flush();
98+
out.close();
99+
}
100+
);
101+
}
102+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.security.user.name=javaboy
2+
spring.security.user.password=123

form-login-3/src/main/resources/static/css/font-awesome-4.7.0/css/font-awesome.min.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)