shiro permissions before and after the separation of management

shiro permissions before and after the separation of management

rear end

rely

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.5.1</version>
</dependency>
<dependency>
    <groupId>org.crazycake</groupId>
    <artifactId>shiro-redis</artifactId>
    <version>3.2.3</version>
</dependency>

redis Configuration

spring:
  redis:
    host: localhost
    port: 6379

UserRealm login authentication and authorization based on roles

public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        UserResult userResult = (UserResult) principals.getPrimaryPrincipal();
        Set<String> role = Collections.singleton(userResult.getRole());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(role);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken UPtoken = (UsernamePasswordToken) token;
        String username = UPtoken.getUsername();
        String password = new String(UPtoken.getPassword());
        User user = userService.queryUserByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            UserResult userResult = new UserResult(user);
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userResult, password, this.getName());
            return info;
        }
        return null;
    }
}

SessionManager obtain header information of the front end of transmission data Authorization

public class SessionManager extends DefaultWebSessionManager {
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {

        String id = WebUtils.toHttp(request).getHeader("Authorization");
        if (StringUtils.isEmpty(id)) {
            return super.getSessionId(request, response);
        } else {
            id = id.replaceAll("Bearer ", "");
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, "header");
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
            return id;
        }
    }
}

ShiroConfig Configuration section of the page and open notes and related redis

@Configuration
public class ShiroConfig {
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }

    @Bean
    public DefaultWebSecurityManager securityManager(UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(userRealm);
        securityManager.setSessionManager(webSessionManager());
        securityManager.setCacheManager(redisCacheManager());
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean bean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);
        //未登录和未授权url
        bean.setLoginUrl("/authError?code=1");
        bean.setUnauthorizedUrl("/authError?code=2");
        Map<String, String> filterMap = new LinkedHashMap<>();
        //无需认证页面
        filterMap.put("/authError", "anon");
        filterMap.put("/login", "anon");
        filterMap.put("/info", "anon");
        filterMap.put("/register", "anon");
        filterMap.put("/guest/**", "anon");
        //swagger页面
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/v2/api-docs/**", "anon");
        filterMap.put("/webjars/springfox-swagger-ui/**", "anon");
        filterMap.put("/configuration/**", "anon");
	//其他页面
        filterMap.put("/**", "authc");
        bean.setFilterChainDefinitionMap(filterMap);

        return bean;
    }
	
    //从配置文件读取redis连接
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;

    public RedisManager redisManager() {
        return new RedisManager();
    }

    public RedisSessionDAO redisSessionDAO() {
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        redisSessionDAO.setRedisManager(redisManager());
        return redisSessionDAO;
    }

    public DefaultWebSessionManager webSessionManager() {
        SessionManager sessionManager = new SessionManager();
        sessionManager.setSessionDAO(redisSessionDAO());
        //禁用cookie
        sessionManager.setSessionIdCookieEnabled(false);
        //禁用url重写
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }

    public RedisCacheManager redisCacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(redisManager());
        return redisCacheManager;
    }


    //开启注解
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn(value = "lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        return creator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}

controller

Unregistered and unauthorized returns the corresponding information to the front end

@RequiresGuest
@GetMapping("/authError")
public Result authError(int code) {
    return code == 1 ? new Result(ResultCode.UNAUTHENTICATED) : new Result(ResultCode.UNAUTHORISE);
}

The controller controls access by verifying the user's role @RequiresRoles

@RequiresRoles(value={"root","管理员"},logical = Logical.OR)
@ApiOperation("用户分页")
@GetMapping("/users/{page}/{size}")
public Result queryUserList(@PathVariable("page") int page, @PathVariable("size") int size) {
    PageHelper.startPage(page, size);
    return new Result(ResultCode.SUCCESS, new PageInfo<>(userService.queryUserList()));
}

Non-privileged user to access an exception is thrown

@ControllerAdvice
public class ExceptionController {
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public Result UnauthorizedException(){
        return new Result(ResultCode.UNAUTHORISE);
    }
}

front end

Installation vuex

npm install vuex --save
npm install js-cookie --save

store / index.js save the token and user information

import Vue from "vue";
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        token: '',
        userInfo: {
            username: '',
            realname: '',
            status: '',
            role: ''
        }
    },
    mutations: {
        set_token(state, token){
            state.token = token;
        },
        set_userInfo(state, userInfo) {
            state.userInfo.username = userInfo.username;
            state.userInfo.realname = userInfo.realname;
            state.userInfo.status = userInfo.status;
            state.userInfo.role = userInfo.role;
        },
        remove_token(state){
            state.token = '';
        },
        remove_userInfo(state){
            state.userInfo.username = '';
            state.userInfo.name = '';
            state.userInfo.status = '';
            state.userInfo.role = ''
        }
    }
})

main.js import store

import store from './store'

new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>',
  render: h => h(App),
})

utils / cookies.js Method Tools

import Cookies from 'js-cookie'

export function getCookies(key) {
    return Cookies.get(key)
}

export function setCookies(key, value) {
    Cookies.set(key, value)
}

export function removeCookies(key) {
    Cookies.remove(key)
}

The method of introducing the desired vue

<script>
    import {getCookies, removeCookies} from "../utils/cookies";
</script>

login.vue

Once you submit the login form with validation, receiving the returned token into the back-end store and cookie, joined in the request Authorization header, the received user information into the store back and cookie, jump to the index page

onSubmit(formName) {
    const _this = this;
    this.$refs[formName].validate((valid) => {
        if (valid) {
            this.form.password=encrypt(this.form.plainpwd);
            this.$axios.post('http://localhost:8081/login', this.form).then(function (resp) {
                if (resp.data.success) {
                    _this.$store.commit('set_token', resp.data.object);
                    setCookies('token', resp.data.object);
                    _this.$axios.post('http://localhost:8081/info', resp.data.object, {
                        headers: {
                            'Authorization': 'Bearer ' + resp.data.object
                        }
                    }).then(function (resp) {
                        // _this.userInfo = resp.data.object;
                        _this.$store.commit('set_userInfo', resp.data.object);
                        setCookies('userInfo', _this.$store.state.userInfo);
                        _this.$message({
                            type: 'success',
                            message: resp.data.message
                        });
                        _this.$router.push('/index')
                    });
                } else {
                    _this.$message({
                        type: 'error',
                        message: resp.data.message
                    });
                }
            })
        } else {
            return false;
        }
    });
},

When accessing the back-end of the front page to be certified by axios, are defined in the Authorization header into the token in

this.$axios.get('http://localhost:8081/solve/' + row.id, {
    headers: {
        'Authorization': 'Bearer ' + getCookies('token')
    }
}

router/permit.js

Permission Validation

  • Logged-in user (cookie exists token)
    • Tourists visit the page --- Back to the index page
    • Ordinary users access to user pages, white pages list, as the root user or administrator release ---
    • Other --- Jump to unauthorized page 10003
  • The user is not logged in (in the absence of token cookie)
    • Tourists visit the release page ---
    • Other --- Jump to unauthorized page 10003
import router from './index'

import {getCookies} from "../utils/cookies"

const guestRouter = ['/login', '/register', '/report'];
const userRouter = ['/index', '/user/add', '/user/records', '/user/profile'];
const whiteRouter = ['/', '/10003', '/404'];
const admin = ['root', '管理员'];

router.beforeEach(function (to, from, next) {
    if (getCookies('token')) {
        if (guestRouter.indexOf(to.path.toLowerCase()) !== -1) {
            next('index')
        } else if (userRouter.indexOf(to.path.toLowerCase()) !== -1 && JSON.parse(getCookies('userInfo')).role === '普通用户' || whiteRouter.indexOf(to.path.toLowerCase()) !== -1 || admin.indexOf(JSON.parse(getCookies('userInfo')).role) !== -1) {
            next()
        } else {
            next('/10003');
        }
    } else {
        if (guestRouter.concat(whiteRouter).indexOf(to.path.toLowerCase()) !== -1) {
            next();
        } else {
            next('/10003');
        }
    }
})

Guess you like

Origin www.cnblogs.com/pinked/p/12661472.html