The authority of filter was defied at properties in most project of pac4j, I need a dynamic authority of request'url which can load data from database, so I changed the project to achieve the function.
You should ensure that the login config works well, when you read next content
- Create a abstract class
CustomAdminAuthGenerator
and extends classAuthorizationGenerator
- Override
generate
method - Create a abstract method
initUserPermissions
and invoke it ingenerate
public abstract class CustomAdminAuthGenerator implements AuthorizationGenerator<CommonProfile> {
@Override
public CommonProfile generate(final WebContext context, final CommonProfile profile) {
profile.addPermissions(this.initUserPermissions(profile.getId()));
profile.clearSensitiveData(); // remove the access token to reduce size and make the remember-me work
profile.setRemembered(true);
return profile;
}
public abstract List<String> initUserPermissions(String credentials);
}
- It has the same steps as before
- There has a method
isAnyAccessAllowed
in the follow class, its in order to change need all to any one of the the permissions
public abstract class CustomPermissionsAuthorizationFilter extends PermissionsAuthorizationFilter {
@Override
public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
// use your services to load permissions
HttpServletRequest req = (HttpServletRequest) request;
List<String> iniPermissions = new ArrayList<>(Arrays.asList((String[]) mappedValue));
iniPermissions.addAll(this.initDynamicPermissions(req.getRequestURI()));
String[] perms = iniPermissions.toArray(new String[0]);
return this.isAnyAccessAllowed(request, response, perms);
}
public abstract List<String> initDynamicPermissions(String requestUri);
}
- You can change your callback url to previous instead of a static url, there is example in
io.buji.pac4j.custom.CustomLogoutFilter
Pac4jRealm
can add your own information toPac4jSubjectFactory
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
can resolve the problem of@Autowire
you can see the demo dynamic-pac4j-demo for this project