Skip to content

Commit d1f49af

Browse files
author
“threedr3am”
committed
Merge branch 'shiro'
2 parents ca02064 + cb6d7a3 commit d1f49af

File tree

8 files changed

+200
-0
lines changed

8 files changed

+200
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>spring</module>
2323
<module>cas</module>
2424
<module>ShardingSphere-UI</module>
25+
<module>shiro</module>
2526
</modules>
2627

2728
<name>learn-java-bug</name>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>1.5.22.RELEASE</version>
9+
<relativePath/>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>auth-bypass-shiro-1.4.1</artifactId>
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<configuration>
20+
<source>7</source>
21+
<target>7</target>
22+
</configuration>
23+
</plugin>
24+
</plugins>
25+
</build>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.apache.shiro</groupId>
35+
<artifactId>shiro-web</artifactId>
36+
<version>1.4.1</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.shiro</groupId>
40+
<artifactId>shiro-spring</artifactId>
41+
<version>1.4.1</version>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.threedr3am.bug.shiro.bypass.auth;
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.threedr3am.bug.shiro.bypass.auth.config;
2+
3+
import com.threedr3am.bug.shiro.bypass.auth.realm.MyRealm;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
import org.apache.shiro.mgt.SecurityManager;
7+
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
8+
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
/**
13+
* @author threedr3am
14+
*/
15+
@Configuration
16+
public class ShiroConfig {
17+
@Bean
18+
MyRealm myRealm() {
19+
return new MyRealm();
20+
}
21+
22+
@Bean
23+
SecurityManager securityManager() {
24+
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
25+
manager.setRealm(myRealm());
26+
return manager;
27+
}
28+
29+
@Bean
30+
ShiroFilterFactoryBean shiroFilterFactoryBean() {
31+
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
32+
bean.setSecurityManager(securityManager());
33+
bean.setLoginUrl("/login");
34+
bean.setSuccessUrl("/index");
35+
bean.setUnauthorizedUrl("/unauthorizedurl");
36+
Map<String, String> map = new LinkedHashMap();
37+
map.put("/login", "anon");
38+
map.put("/bypass", "authc");
39+
bean.setFilterChainDefinitionMap(map);
40+
return bean;
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.threedr3am.bug.shiro.bypass.auth.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RequestMethod;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
/**
8+
* @author threedr3am
9+
*/
10+
@RestController
11+
public class BypassTestController {
12+
13+
/**
14+
* todo 过滤器配置(参考ShiroConfig)中bypass映射认证过滤器最后一个URI字符没有/,使用spring和shiro对资源的解析不一致进行bypass
15+
*
16+
* 例:配置"/bypass", "authc",请求http://localhost:8080/bypass/
17+
*
18+
* shiro <= 1.4.1
19+
*
20+
* @return
21+
*/
22+
@RequestMapping(value = "/bypass", method = RequestMethod.GET)
23+
public String bypass() {
24+
return "bypass";
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.threedr3am.bug.shiro.bypass.auth.controller;
2+
3+
import org.apache.shiro.SecurityUtils;
4+
import org.apache.shiro.authc.AuthenticationException;
5+
import org.apache.shiro.authc.UsernamePasswordToken;
6+
import org.apache.shiro.subject.Subject;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* @author threedr3am
13+
*/
14+
@RestController
15+
public class LoginController {
16+
17+
@RequestMapping(value = "/login", method = RequestMethod.POST)
18+
public String login(String username, String password) {
19+
Subject subject = SecurityUtils.getSubject();
20+
try {
21+
subject.login(new UsernamePasswordToken(username, password));
22+
return "登录成功!";
23+
} catch (AuthenticationException e) {
24+
e.printStackTrace();
25+
return "登录失败!";
26+
}
27+
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.threedr3am.bug.shiro.bypass.auth.realm;
2+
3+
import org.apache.shiro.authc.AuthenticationException;
4+
import org.apache.shiro.authc.AuthenticationInfo;
5+
import org.apache.shiro.authc.AuthenticationToken;
6+
import org.apache.shiro.authc.SimpleAuthenticationInfo;
7+
import org.apache.shiro.authc.UnknownAccountException;
8+
import org.apache.shiro.authz.AuthorizationInfo;
9+
import org.apache.shiro.realm.AuthorizingRealm;
10+
import org.apache.shiro.subject.PrincipalCollection;
11+
12+
/**
13+
* @author threedr3am
14+
*/
15+
public class MyRealm extends AuthorizingRealm {
16+
@Override
17+
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
18+
return null;
19+
}
20+
@Override
21+
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
22+
String username = (String) token.getPrincipal();
23+
if (!"threedr3am".equals(username)) {
24+
throw new UnknownAccountException("账户不存在!");
25+
}
26+
return new SimpleAuthenticationInfo(username, "123456", getName());
27+
}
28+
}

shiro/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
<parent>
6+
<artifactId>learn-java-bug</artifactId>
7+
<groupId>com.xyh</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>shiro</artifactId>
13+
<packaging>pom</packaging>
14+
15+
16+
</project>

0 commit comments

Comments
 (0)