Skip to content

Commit

Permalink
security oauth2 使用
Browse files Browse the repository at this point in the history
  • Loading branch information
tuacy committed Dec 3, 2019
1 parent 514253a commit 64d79a7
Show file tree
Hide file tree
Showing 25 changed files with 660 additions and 53 deletions.
19 changes: 19 additions & 0 deletions security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

<artifactId>security</artifactId>

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

<dependencies>

Expand All @@ -24,6 +27,13 @@
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- 允许使用非严格的 HTML 语法 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
Expand Down Expand Up @@ -96,4 +106,13 @@

</dependencies>

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

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
* @version: 1.0
* @Description: OAuth2认证授权服务配置
*/
@Configuration
@EnableAuthorizationServer
//@Configuration
//@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

private AuthenticationManager authenticationManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
* @version: 1.0
* @Description:
*/
@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
//@Configuration
//@EnableCaching
//@EnableConfigurationProperties(CacheProperties.class)
public class CacheManagerConfig {

private final CacheProperties cacheProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* @version: 1.0
* @Description:
*/
@Configuration
@EnableResourceServer
//@Configuration
//@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

private static final String RESOURCE_IDS = "order";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.tuacy.security.config;

import org.springframework.context.annotation.Bean;
import com.tuacy.security.security.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.DigestUtils;

/**
* @name: WebSecurityConfig
Expand All @@ -20,30 +21,67 @@
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
private UserDetailsServiceImpl userService;

@Autowired
public void setUserService(UserDetailsServiceImpl userService) {
this.userService = userService;
}

/**
* 注入AuthenticationManager接口,启用OAuth2密码模式
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

//校验用户
auth.userDetailsService(userService).passwordEncoder(new PasswordEncoder() {
//对密码进行加密
@Override
public String encode(CharSequence charSequence) {
System.out.println(charSequence.toString());
return DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
}

//对密码进行判断匹配
@Override
public boolean matches(CharSequence charSequence, String s) {
String encode = DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
return s.equals(encode);
}
});

}

/**
* 通过HttpSecurity实现Security的自定义过滤配置
*/
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.requestMatchers().anyRequest()
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "index", "/login", "/login-error", "/401", "/css/**", "/js/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").failureUrl("/login-error")
.and()
.authorizeRequests()
.antMatchers("/oauth/**").permitAll();
.exceptionHandling().accessDeniedPage("/401");
http.logout().logoutSuccessUrl("/");
}


// /**
// * 注入AuthenticationManager接口,启用OAuth2密码模式
// */
// @Bean
// @Override
// public AuthenticationManager authenticationManagerBean() throws Exception {
// return super.authenticationManagerBean();
// }
//
// /**
// * 通过HttpSecurity实现Security的自定义过滤配置
// */
// @Override
// protected void configure(HttpSecurity httpSecurity) throws Exception {
// httpSecurity
// .requestMatchers().anyRequest()
// .and()
// .authorizeRequests()
// .antMatchers("/oauth/**").permitAll();
// }

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.tuacy.security.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @name: UserController
Expand All @@ -11,13 +12,43 @@
* @version: 1.0
* @Description:
*/
@RestController
@RequestMapping("]user")
@Controller
public class UserController {

@GetMapping
public String getUsers() {
return "Hello Spring Security";
@RequestMapping("/")
public String root() {
return "redirect:/index";
}

@RequestMapping("/index")
public String index() {
return "index";
}

@RequestMapping("/login")
public String login() {
return "login";
}

@RequestMapping("/login-error")
public String loginError(Model model) {
model.addAttribute("loginError", true);
return "login";
}

@GetMapping("/401")
public String accessDenied() {
return "401";
}

@GetMapping("/user/common")
public String common() {
return "user/common";
}

@GetMapping("/user/admin")
public String admin() {
return "user/admin";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tuacy.security.entity.model;

import org.springframework.security.core.GrantedAuthority;

/**
* @name: RoleDetailBo
* @author: tuacy.
* @date: 2019/12/2.
* @version: 1.0
* @Description:
*/
public class RoleDetailBo implements GrantedAuthority {
private String id;
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String getAuthority() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.tuacy.security.entity.model;

/**
* @name: RolePermisson
* @author: tuacy.
* @date: 2019/12/2.
* @version: 1.0
* @Description:
*/
public class RolePermisson {

private String url;
private String roleName;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getRoleName() {
return roleName;
}

public void setRoleName(String roleName) {
this.roleName = roleName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tuacy.security.mapper;

import com.tuacy.security.entity.model.RolePermisson;

import java.util.List;

/**
* @name: UserManageMapper
* @author: tuacy.
* @date: 2019/11/28.
* @version: 1.0
* @Description:
*/
public interface PermissionMapper {


List<RolePermisson> getRolePermissions();

}
19 changes: 19 additions & 0 deletions security/src/main/java/com/tuacy/security/mapper/RoleMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tuacy.security.mapper;

import com.tuacy.security.entity.model.RoleDetailBo;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* @name: RoleMapper
* @author: tuacy.
* @date: 2019/12/2.
* @version: 1.0
* @Description:
*/
public interface RoleMapper {

List<RoleDetailBo> getRolesByUserId(@Param("userId") Long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public interface UserManageMapper {
/**
* 根据用户名获取用户信息
*/
UserInfoPo findUserInfoByName(@Param("userName") String userName);
UserInfoPo loadUserByUsername(@Param("userName") String userName);

}
Loading

0 comments on commit 64d79a7

Please sign in to comment.