Giter Club home page Giter Club logo

springstudy's Introduction

Yang Jae Seo ๐Ÿคช

What does not kill me, makes me stronger

I'm double majoring in Chemical engineering and Computer engineering
I'm interested in Backend, DevOps Develop.
Looking for someone to be strong with me.

Activity


Solved.ac Profile


๐Ÿ›  Tech Stack ๐Ÿ› 


Language ย  ย  ย  ย  ย  ย 
FE ย  ย  ย  ย 
BE ย  ย 
Community ย 
Database ย  ย 
Deployment ย  ย  ย  ย  ย  ย 
Communication ย  ย  ย  ย 
Project ๐Ÿ’ฐ ์ง€์ถœ ๊ด€๋ฆฌ SNS ํ”Œ๋žซํผ, Pennyway
๐Ÿถ ๋ฐ˜๋ ค๋™๋ฌผ ์ผ€์–ด ์„œ๋น„์Šค, Fit a Pet
๐Ÿ“š ํŒ”๋งŒ์ฝ”๋”ฉ๊ฒฝ
๐Ÿ’ฌ ์นด์นด์˜ค ์ฑ— CI
๐Ÿงฎ ๋ชจ์ž„ ์ •์‚ฐ ์„œ๋น„์Šค, Trip Tip
github-snake

springstudy's People

Contributors

psychology50 avatar

Watchers

 avatar

springstudy's Issues

AccessDeninedHandler

@Log4j2
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
//    private String errorPage;
//    public void setErrorPage(String errorPage) {
//        this.errorPage = errorPage;
//    }


    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        String accessDeniedUrl = (String) request.getSession().getAttribute("accessDeniedUrl");
        if (accessDeniedUrl != null) {
            log.info("accessDeniedUrl : {}", accessDeniedUrl);
            log.info("accessDeniedException : {}", accessDeniedException.getMessage());
            log.info("request method : {}", request.getMethod());

//            response.sendRedirect(accessDeniedUrl + "?exception=" + accessDeniedException.getMessage()); // ์ €์žฅ๋œ API ์ฃผ์†Œ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");

            PrintWriter writer = response.getWriter();
            writer.write("{\"error\": \"" + accessDeniedException.getMessage() + "\"}");
            writer.flush();

        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied");
        }

//        String deniedUrl = errorPage + "?exception=" + accessDeniedException.getMessage();
//        response.sendRedirect(deniedUrl);
    }
}

์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน๊ธด ํ–ˆ์ง€๋งŒ ์ •๋ง ์œ ์ตํ•œ ๋‚ด์šฉ๋“ค์„ ๋งŽ์ด ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ๋‹ค!!!
ํŠนํžˆ ์‚ฌ์šฉ์ž๊ฐ€ Delete๋กœ ์š”์ฒญํ•œ ์ •๋ณด๋ฅผ response.sendRedirect()๋กœ ์žฌ์ „์†ก์„ ์œ ๋ฐœํ•˜๊ฒŒ ํ•  ๊ฒฝ์šฐ, ๋‚ด๊ฐ€ ์ง€์ •ํ•ด์ค€ ๊ฒฝ๋กœ๋กœ DELETE ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๋Œ€์ฐธ์‚ฌ๊ฐ€ ์ œ๋ฒ• ๊ณจ๋•Œ๋ ธ๋‹ค.
๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด Redirect๋ฅผ ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ์ž์œ ๋กญ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌํ–ˆ๋‹ค.
๋‚˜์ค‘์— API๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ ๋„์›€์ด ๋˜๋Š” ๊ธฐ์ˆ ์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

@PreAuthorize

์žฅ๊ณ ์—์„œ๋Š” ํ˜„์žฌ request๋ฅผ ๋ณด๋‚ธ ํด๋ผ์ด์–ธํŠธ ์ •๋ณด๋ฅผ ํ† ๋Œ€๋กœ ํŠน์ • ๋ฉ”์„œ๋“œ ์ ‘๊ทผ ๊ถŒํ•œ์„ ์ƒ์„ธํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์‰ฝ๊ฒŒ ์ œ๊ณตํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.
๋‹จ์ˆœํžˆ ๋กœ๊ทธ์ธ ์œ ๋ฌด๋‚˜ ADMIN, USER๊ฐ€ ์•„๋‹ˆ๋ผ 'ํŠน์ • ๊ทธ๋ฃน์— ์†Œ์†๋˜์–ด ์žˆ๋Š”๊ฐ€?'๋ฅผ ๋”ฐ์ง€๋ ค๋ฉด ์ปค์Šคํ…€ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ–ˆ๋‹ค.
์Šคํ”„๋ง์—์„œ ์ด ๊ธฐ๋Šฅ์„ ๋•๋Š” ๊ฒƒ์ด PreAuthorize์ธ๋ฐ, ๋ฌผ๋ก  ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์œ„ํ•ด์„œ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์ปค์Šคํ…€ํ•ด์•ผ๋งŒ ํ–ˆ๋‹ค.
์ด ๊ณผ์ •์—์„œ ๋„ˆ๋ฌด ์˜ค๋žœ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜์–ด ๊ฒฐ๊ณผ์ ์œผ๋กœ ์‹คํŒจํ–ˆ๋‹ค.

public class CustomSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
    private ApplicationContext applicationContext;
    private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();

    @Override
    protected MethodSecurityExpressionOperations createSecurityExpressionRoot(
            Authentication authentication, MethodInvocation invocation
    ) {
        CustomMethodSecurityExpressionRoot root = new CustomMethodSecurityExpressionRoot(authentication);
        root.setThis(invocation.getThis());
        root.setPermissionEvaluator(getPermissionEvaluator());
        root.setTrustResolver(this.trustResolver);
//        root.setRoleHierarchy(getRoleHierarchy());
        root.setReturnObject(invocation.getThis());
        return root;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        super.setApplicationContext(applicationContext);
        this.applicationContext = applicationContext;
    }
}

๋‚ด ์ถ”์ธก์ด์ง€๋งŒ MethodSecurityExpressionRoot๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์–ด์„œ DefaultMethodSecurityExpressionHandler๋ฅผ ํ†ตํ•ด ๊ฐ„์ ‘์ ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋ฒ„์ „์ด ๋ฐ”๋€Œ๋ฉด์„œ ๋ญ”๊ฐ€ ๋ฐฉ๋ฒ•์ด ๋ฐ”๋€ ๊ฒŒ ์•„๋‹๊นŒ ์‹ถ๋‹ค.
์˜จ๊ฐ– ๋ธ”๋กœ๊ทธ๋ฅผ ๋‹ค ๋’ค์ง€๊ณ  ๋ช‡ ์‹œ๊ฐ„์„ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋„์ €ํžˆ ์‹œ๊ฐ„์ด ๋ถ€์กฑํ•˜๋‹ค๋Š” ํŒ๋‹จ์ด ๋˜์–ด ๊ผผ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ์ผ๋‹จ ํšŒํ”ผํ–ˆ๋‹ค.

@Service("securityService")
@RequiredArgsConstructor
@Log4j2
public class SecurityService {

    private final UserSearchService userSearchService;
    private final Logger logger = Logger.getLogger(SecurityService.class.getName());
    Authentication authentication;
    ServletRequestAttributes attributes;

    public boolean isLeader(Long group_id) {
        authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = userSearchService.findUserById(((User) authentication.getPrincipal()).getUser_id()).orElseThrow(()
            -> new RuntimeException("ํ•ด๋‹น ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));
        log.info("isLeader = {}", user);

        boolean is_leader = user.getIs_leader().stream().anyMatch(group -> group.getGroup_id().equals(group_id));

        if (!is_leader)
            exception_handling();
        return is_leader;
    }

    public boolean isMember(Long group_id) {
        authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = userSearchService.findUserById(((User) authentication.getPrincipal()).getUser_id()).orElseThrow(()
                -> new RuntimeException("ํ•ด๋‹น ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));
        log.info("isMember = {}", user);

        boolean is_member = user.getMembers().stream().anyMatch(member -> member.getGroup().getGroup_id().equals(group_id));

        if (!is_member)
            exception_handling();
        return is_member;
    }

    public boolean isPayer(Long receipt_id) {
        authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = userSearchService.findUserById(((User) authentication.getPrincipal()).getUser_id()).orElseThrow(()
                -> new RuntimeException("ํ•ด๋‹น ์œ ์ €๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));
        log.info("isPayer = {}", user);

        boolean is_payer = user.getPay_receipts().stream().anyMatch(receipt -> receipt.getReceipt_id().equals(receipt_id));

        if (!is_payer)
            exception_handling();
        return is_payer;
    }

    private void exception_handling() {
        attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
//                String currentUrl = attributes.getRequest().getRequestURI();
//                String currentUrl = attributes.getRequest().getRequestURL().toString();
            // XXX: ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋Š” url. ์ข‹์ง€ ์•Š๋‹ค.
            String currentUrl = attributes.getRequest().getHeader("referer");
            attributes.getRequest().getSession().setAttribute("accessDeniedUrl", currentUrl);
        }
        log.info("attributes = {}", attributes);

        throw new AccessDeniedException("You do not have permission to access this resource.");
    }
}
    @DeleteMapping("/{receipt_id}/delete")
    @PreAuthorize("@securityService.isPayer(#receipt_id)")
    public ResponseEntity<String> delete(@PathVariable(value = "group_id") Long group_id,
                                         @PathVariable(value = "meeting_id") Long meeting_id,
                                         @PathVariable(value = "receipt_id") Long receipt_id) {
        receiptManageService.delete(receipt_id);
        return ResponseEntity.ok("receipt delete success");
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.