配置Spring Security.

This commit is contained in:
Saturneric 2020-02-05 17:36:50 +08:00
parent ea2ce3d1d4
commit 750f5aec4c
9 changed files with 56 additions and 11 deletions

View File

@ -13,6 +13,6 @@ public class ASEPasswordEncoder implements PasswordEncoder {
@Override @Override
public boolean matches(CharSequence charSequence, String s) { public boolean matches(CharSequence charSequence, String s) {
return s.equals(DigestUtils.sha256Hex(charSequence.toString())); return s.equals(charSequence.toString());
} }
} }

View File

@ -0,0 +1,24 @@
package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.FunctionalPermissionContainer;
import com.codesdream.ase.model.permission.Tag;
import com.codesdream.ase.model.permission.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
@Component
public class UserRolesListGenerator {
public Collection<GrantedAuthority> GenerateRoles(User user){
Collection<GrantedAuthority> authorities = new ArrayList<>();
for(Tag tag : user.getTags()){
for(FunctionalPermissionContainer functionalPermissionContainer : tag.getFunctionalPermissionContainers()){
authorities.add(new SimpleGrantedAuthority(functionalPermissionContainer.getName()));
}
}
return authorities;
}
}

View File

@ -43,7 +43,7 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(aseUserDetailService).passwordEncoder(asePasswordEncoder); auth.inMemoryAuthentication().withUser("user").password("password").roles("普通用户");
} }
@Override @Override

View File

@ -1,11 +1,10 @@
package com.codesdream.ase.model.permission; package com.codesdream.ase.model.permission;
import com.codesdream.ase.component.UserRolesListGenerator;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -93,9 +92,8 @@ public class User implements UserDetails {
@Override @Override
public Collection<? extends GrantedAuthority> getAuthorities() { public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>(); UserRolesListGenerator userRolesListGenerator = new UserRolesListGenerator();
authorities.add(new SimpleGrantedAuthority("user")); return userRolesListGenerator.GenerateRoles(this);
return authorities;
} }
@Override @Override

View File

@ -0,0 +1,12 @@
package com.codesdream.ase.repository;
import com.codesdream.ase.model.permission.FunctionalPermissionContainer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface FunctionalPermissionRepository extends CrudRepository<FunctionalPermissionContainer, Integer> {
Optional<FunctionalPermissionRepository> findByName(String name);
}

View File

@ -0,0 +1,10 @@
package com.codesdream.ase.repository;
import com.codesdream.ase.model.permission.ScopePermissionContainer;
import org.springframework.data.repository.CrudRepository;
import java.util.Optional;
public interface ScopePermissionRepository extends CrudRepository<ScopePermissionContainer, Integer> {
Optional<ScopePermissionRepository> findByName(String name);
}

View File

@ -6,6 +6,7 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
@Repository @Repository
public interface TagRepository extends CrudRepository<Tag, Integer> { public interface TagRepository extends CrudRepository<Tag, Integer> {

View File

@ -5,9 +5,6 @@ spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.encoding=UTF-8
spring.security.user.name=user
spring.security.user.password=123
spring.jpa.generate-ddl=false spring.jpa.generate-ddl=false
spring.jpa.show-sql=true spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create spring.jpa.hibernate.ddl-auto=create

View File

@ -14,7 +14,7 @@
<div class="card-header"> <div class="card-header">
<h4 class="card-title">登录</h4> <h4 class="card-title">登录</h4>
</div> </div>
<form action="/login" method="post"> <form th:action="@{/login}" method="post">
<div class="card-body"> <div class="card-body">
<div class="form-group"> <div class="form-group">
<label for="username">用户名</label> <label for="username">用户名</label>
@ -29,6 +29,9 @@
<button type="submit" class="btn btn-primary btn-block"><b>登录</b></button> <button type="submit" class="btn btn-primary btn-block"><b>登录</b></button>
</div> </div>
</form> </form>
<div th:if="${param.error}">
用户名或密码错误
</div>
</div> </div>
</div> </div>
</div> </div>