Skip to content

Commit ee9ddec

Browse files
committed
add qq login demo
1 parent 771b414 commit ee9ddec

File tree

13 files changed

+777
-0
lines changed

13 files changed

+777
-0
lines changed

security-oauth2-qq/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.spring4all.anoy</groupId>
8+
<artifactId>security-oauth2-qq</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<description>Spring Security 基础 DEMO ( QQ 登录 )- 来源于 SpringForAll 社区(http://spring4all.com)</description>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>2.0.0.M1</version>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-security</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-configuration-processor</artifactId>
38+
<optional>true</optional>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.jsoup</groupId>
43+
<artifactId>jsoup</artifactId>
44+
<version>1.10.2</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>com.alibaba</groupId>
49+
<artifactId>fastjson</artifactId>
50+
<version>1.2.32</version>
51+
</dependency>
52+
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-maven-plugin</artifactId>
60+
<version>1.5.3.RELEASE</version>
61+
<executions>
62+
<execution>
63+
<goals>
64+
<goal>repackage</goal>
65+
</goals>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
<repositories>
73+
<repository>
74+
<id>spring-milestones</id>
75+
<name>Spring Milestones</name>
76+
<url>https://repo.spring.io/libs-milestone</url>
77+
<snapshots>
78+
<enabled>false</enabled>
79+
</snapshots>
80+
</repository>
81+
</repositories>
82+
83+
84+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.spring4all;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.spring4all.config;
2+
3+
import com.spring4all.filter.qq.QQAuthenticationFilter;
4+
import com.spring4all.filter.qq.QQAuthenticationManager;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
9+
10+
@EnableWebSecurity
11+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
12+
13+
/**
14+
* 匹配 "/" 路径,不需要权限即可访问
15+
* 匹配 "/user" 及其以下所有路径,都需要 "USER" 权限
16+
* 登录地址为 "/login",登录成功默认跳转到页面 "/user"
17+
* 退出登录的地址为 "/logout",退出成功后跳转到页面 "/login"
18+
* 默认启用 CSRF
19+
*/
20+
@Override
21+
protected void configure(HttpSecurity http) throws Exception {
22+
http
23+
.authorizeRequests()
24+
.antMatchers("/").permitAll()
25+
.antMatchers("/user/**").hasRole("USER")
26+
.and()
27+
.formLogin().loginPage("/login").defaultSuccessUrl("/user")
28+
.and()
29+
.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
30+
31+
// 在 UsernamePasswordAuthenticationFilter 前添加 QQAuthenticationFilter
32+
http.addFilterAt(qqAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
33+
34+
}
35+
36+
/**
37+
* 自定义 QQ登录 过滤器
38+
*/
39+
private QQAuthenticationFilter qqAuthenticationFilter(){
40+
QQAuthenticationFilter authenticationFilter = new QQAuthenticationFilter("/login/qq");
41+
authenticationFilter.setAuthenticationManager(new QQAuthenticationManager());
42+
return authenticationFilter;
43+
}
44+
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.spring4all.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@Controller
7+
public class HomeController {
8+
9+
@GetMapping({"/", "/index", "/home"})
10+
public String root(){
11+
return "index";
12+
}
13+
14+
@GetMapping("/login")
15+
public String login(){
16+
return "login";
17+
}
18+
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.spring4all.controller;
2+
3+
import com.spring4all.domain.QQUser;
4+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
5+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
10+
@Controller
11+
public class UserController {
12+
13+
@GetMapping("/user")
14+
public String user(@AuthenticationPrincipal UsernamePasswordAuthenticationToken userAuthentication, Model model){
15+
QQUser user = (QQUser) userAuthentication.getPrincipal();
16+
model.addAttribute("username", user.getNickname());
17+
model.addAttribute("avatar", user.getAvatar());
18+
return "user/user";
19+
}
20+
21+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.spring4all.domain;
2+
3+
public class QQUser {
4+
5+
/**
6+
* 昵称
7+
*/
8+
private String nickname;
9+
10+
/**
11+
* 性别
12+
*/
13+
private String gender;
14+
15+
/**
16+
* 省份
17+
*/
18+
private String province;
19+
20+
/**
21+
* 出生年
22+
*/
23+
private String year;
24+
25+
/**
26+
* 头像
27+
*/
28+
private String avatar;
29+
30+
public String getNickname() {
31+
return nickname;
32+
}
33+
34+
public void setNickname(String nickname) {
35+
this.nickname = nickname;
36+
}
37+
38+
public String getGender() {
39+
return gender;
40+
}
41+
42+
public void setGender(String gender) {
43+
this.gender = gender;
44+
}
45+
46+
public String getProvince() {
47+
return province;
48+
}
49+
50+
public void setProvince(String province) {
51+
this.province = province;
52+
}
53+
54+
public String getYear() {
55+
return year;
56+
}
57+
58+
public void setYear(String year) {
59+
this.year = year;
60+
}
61+
62+
public String getAvatar() {
63+
return avatar;
64+
}
65+
66+
public void setAvatar(String avatar) {
67+
this.avatar = avatar;
68+
}
69+
}

0 commit comments

Comments
 (0)