1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.samples.spring.ui;
20
21 import java.awt.*;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.WindowAdapter;
25 import java.awt.event.WindowEvent;
26 import javax.swing.*;
27
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.core.io.ClassPathResource;
30
31 import org.apache.shiro.authz.AuthorizationException;
32 import org.apache.shiro.samples.spring.SampleManager;
33
34
35
36
37
38
39
40
41
42
43 public class WebStartView implements ActionListener, InitializingBean {
44
45
46
47
48
49
50
51
52 private SampleManager sampleManager;
53 private JTextField valueField;
54 private JButton saveButton;
55 private JButton refreshButton;
56 private JButton secureMethod1Button;
57 private JButton secureMethod2Button;
58 private JButton secureMethod3Button;
59 private JFrame frame;
60
61
62
63
64
65
66
67
68
69 public void setSampleManager(SampleManager sampleManager) {
70 this.sampleManager = sampleManager;
71 }
72
73
74
75
76 public void afterPropertiesSet() throws Exception {
77 ClassPathResource resource = new ClassPathResource("logo.png");
78 ImageIcon icon = new ImageIcon(resource.getURL());
79 JLabel logo = new JLabel(icon);
80
81 valueField = new JTextField(20);
82 updateValueLabel();
83
84 saveButton = new JButton("Save Value");
85 saveButton.addActionListener(this);
86
87 refreshButton = new JButton("Refresh Value");
88 refreshButton.addActionListener(this);
89
90 JPanel valuePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
91 valuePanel.add(valueField);
92 valuePanel.add(saveButton);
93 valuePanel.add(refreshButton);
94
95 secureMethod1Button = new JButton("Method #1");
96 secureMethod1Button.addActionListener(this);
97
98 secureMethod2Button = new JButton("Method #2");
99 secureMethod2Button.addActionListener(this);
100
101 secureMethod3Button = new JButton("Method #3");
102 secureMethod3Button.addActionListener(this);
103
104 JPanel methodPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
105 methodPanel.add(secureMethod1Button);
106 methodPanel.add(secureMethod2Button);
107 methodPanel.add(secureMethod3Button);
108
109 frame = new JFrame("Apache Shiro Sample Application");
110 frame.setSize(500, 200);
111
112 Container panel = frame.getContentPane();
113 panel.setLayout(new BorderLayout());
114 panel.add(logo, BorderLayout.NORTH);
115 panel.add(valuePanel, BorderLayout.CENTER);
116 panel.add(methodPanel, BorderLayout.SOUTH);
117
118 frame.setVisible(true);
119 frame.addWindowListener(new WindowAdapter() {
120 public void windowClosing(WindowEvent e) {
121 System.exit(0);
122 }
123 });
124 }
125
126 private void updateValueLabel() {
127 valueField.setText(sampleManager.getValue());
128 }
129
130 public void actionPerformed(ActionEvent e) {
131 try {
132
133 if (e.getSource() == saveButton) {
134 sampleManager.setValue(valueField.getText());
135
136 } else if (e.getSource() == refreshButton) {
137 updateValueLabel();
138
139 } else if (e.getSource() == secureMethod1Button) {
140 sampleManager.secureMethod1();
141 JOptionPane.showMessageDialog(frame, "Method #1 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
142
143 } else if (e.getSource() == secureMethod2Button) {
144 sampleManager.secureMethod2();
145 JOptionPane.showMessageDialog(frame, "Method #2 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
146 } else if (e.getSource() == secureMethod3Button) {
147 sampleManager.secureMethod3();
148 JOptionPane.showMessageDialog(frame, "Method #3 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
149
150 } else {
151 throw new RuntimeException("Unexpected action event from source: " + e.getSource());
152 }
153
154 } catch (AuthorizationException ae) {
155 JOptionPane.showMessageDialog(frame, "Unauthorized to perform action: " + ae.getMessage(), "Unauthorized", JOptionPane.WARNING_MESSAGE);
156 }
157 }
158 }